I’ve seen a lot of people release whitelist scripts but, only for Databases. I couldn’t find one that worked without a Database so, I decided to make my own that uses a file.
I made it so you can change the file name the resource looks for (so you can have multiple whitelist servers running with the same instance of the resource), just set the convar in the server.cfg file (i.e. set enfer.whitelist "second-whitelist.json"
). Make sure this file exists in the same directory as the Lua files. If it’s not, it will not load them.
You can download the files here: whitelist.zip (1.6 KB)
If you just want to look at how it’s written, look below.
__resource.lua
resource_manifest_version "44febabe-d386-4d18-afbe-5e627f4af937"
server_scripts {
'whitelist.lua'
}
whitelist.lua
--[[
Simple whitelist script to check if a player's identifier is inside
a file.
Copyright 2018, Tony Enfer, All rights reserved.
You may modify this resource but NOT, under any circumstances, redistribute it.
]]
-- The whitelist array
local whitelistArray = {}
-- Read the "whitelist.json" file.
local data = LoadResourceFile(GetCurrentResourceName(), GetConvar("enfer.whitelist", "whitelist.json"))
-- If we got data from it, parse it and store it.
if data then
whitelistArray = json.decode(data)
print("Loaded " .. #whitelistArray .. " whitelisted identifiers")
end
--[[
When a player joins, check if they're in the whitelistArray.
If they are, allow them to join. Otherwise don't let them.
]]
AddEventHandler("playerConnecting", function(playerName, setKickReason, deferrals)
-- Tell the connection to defer until we have done
deferrals.defer()
-- Just in case "source" gets lost, store it
local s = source
-- If the player has been allowed to join the server
local joined = false
-- Tell the user we're checking stuff (not shown for long)
deferrals.update("Please wait, checking whitelist")
-- Needed, not sure why.
Wait(100)
-- A bunch of variables for calculating percentage
local totalInWhitelist = #whitelistArray -- Total no of IDs in the array
local noIdentifiers = #GetPlayerIdentifiers(s) -- How many Identifiers the player has (usually 2 or 3)
local currentId = 0 -- The index of the id we're checking
local totalChecksNeeded = totalInWhitelist * noIdentifiers -- The total number of interations we need (to check the entire array)
-- Loop though the player's identifiers
for myIdx,identifier in pairs(GetPlayerIdentifiers(s)) do
-- Loop through the whitelist array
for wIdx,i in ipairs(whitelistArray) do
-- Check if the player exists in the array.
if(string.lower(i) == string.lower(identifier))then
deferrals.done() -- They're in it... Let them in!
joined = true
break -- no longer need to loop
end
-- Tell user how much we've done
deferrals.update(string.format("Checking whitelist: %.2f%%", (currentId / totalChecksNeeded)*100))
Wait(1)
currentId = currentId +1
end
end
if joined then
return
end
-- Tell them to get lost. They're not in the array!
deferrals.done("You are not whitelisted.")
end)
And an example JSON file
[
"steam:11000010abeb8a3",
"ip:127.0.0.1"
]