Hey, I’m trying to add a white list to my game. I currently have the following:
local whitelist = {'names'}
game.Players.PlayerAdded:Connect(function(player)
for i, v in pairs(whitelist) do
if v == player.Name then
print("Whitelisted")
else
player:Kick()
end
end
end)
Instead of letting the people who are on the list in the game seems to kick everyone?
I have tried many ways including an if statement to see if the player is not on the whitelist and then kick that way but nothing seems to be working?
Kicks everyone because someone who passes one iteration won’t pass another due to the way you’ve written your for loop. This isn’t a good way to do this anyway - as in, you shouldn’t be running loops at all here - and you can simplify this majorly.
-- Canonical way to get a service
local Players = game:GetService("Players")
-- Use UserIds instead; usernames can change, UserIds cannot
local WHITELIST = {"names"}
Players.PlayerAdded:Connect(function (player)
if not table.find(WHITELIST, player.Name) then
player:Kick()
end
end)
Dictionaries also work where you can do lookups on a name.
I have created whitelisting systems myself, and here is something I have learned from insight.
You actually have the structure for a whitelist yourself, albeit with two issues:
You should never be checking the player’s authenticity by their player name for it can be changed via roblox, (Not that it’s critical here, but in general when it comes to fetching player info, data etc)
Check the player via theirPlayer.UserId as it is unique, static, and never will change.
As for the table, you are going to need to use a dictionary form of table: More like this:
local whitelist = { ['665958081'] = "Whatever you want this to be"} -- if you'd like any more users, simply drop a line and insert another bracketed key as so:
local whitelist = {['665958081'] = "Blah", ['326346272'] = "Blah"} etc.
Now to check, all you simply have to change in your loop is the if statement: change if v == Player.Na me then to `if whitelist[tostring(Player.UserId)] then print(“Whitelisted”)
and then your else function should handle as desired if a player’s Id’ isn’t on par with your whitelisted id’s.