I’ve been trying to create a whitelist system using Json on my website for easy update etc.
I am trying to figure out why my code wouldn’t work.
Code Summary
local HttpService = game:GetService("HttpService")
local url = "http://rbx.yulxvol.com/whitelist"
local js_table = HttpService:GetAsync(url)
local whitelist = HttpService:JSONDecode(js_table)
local array = whitelist.userids
local function checkTheList(array, val)
for k,v in pairs(array) do
if v == val then return true end
end
end
game.Players.PlayerAdded:Connect(function(player)
if checkTheList(array, player.UserId) then
print("Can Joined")
else
print("Kicked")
end
end)
Your website is returning UserIds as strings, but player.UserId is an integer. They’ll never be equal. So you either need to convert player.UserId to a string first or convert your array to numbers.
Ok, so the other problem is that your PlayerAdded event is probably not being hooked up quickly enough. You are calling your website first, which takes a little bit. So by the time it reaches the line to hook up the event, your player might already be in the game.
Therefore, it’s useful to have another routine that scans through the players in the game alongside hooking up the PlayerAdded event:
function PlayerAdded(player)
if checkTheList(array, tostring(player.UserId)) then
print("Can Joined")
else
print("Kicked")
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for _,player in pairs(game.Players:GetPlayers()) do
spawn(function() PlayerAdded(player) end)
end