Whitelist script only checks the first player in the list

Hello, so this whitelist script I made only checks the first person on the list, but I want the list to include everyone, any help?

local whitelist = {
	"664880421",
	"2513627682",
}

game.Players.PlayerAdded:Connect(function(player)
		for i,v in pairs(whitelist) do
			if v ~= player.UserId then
			player:Kick("You're not allowed here.")
			end
	end
end)
1 Like

Have you made sure the userids are correct? I don’t see anything wrong with this code.

Yeah, I copied them directly from the profiles. Yet it still doesn’t work, try using ROBLOX’s user id or something first and then yours and it won’t let you in.

try changing the code to:

local whitelist = {
	664880421,
	2513627682,
}

game.Players.PlayerAdded:Connect(function(player)
		for i,v in pairs(whitelist) do
			if v ~= player.UserId then
			player:Kick("You're not allowed here.")
			end
	   end
end)

What’s happening is that strings are not the same as integers.

player.UserId is of type integer, not string

The player cannot be equal to both user Id’s, this is the issue.
You can try this:

local whitelist = {
	"664880421",
	"2513627682",
}

game.Players.PlayerAdded:Connect(function(player)
        local found = false
		for i,v in pairs(whitelist) do
			 if v == player.UserId then
			    found = true
			 end
	     end
         if not found then
             player:Kick("You're not allowed here.")
         end
end)

same as Farley120’s script

Your kick function is being run every time the userId does not match. This means it will check the first element. If the first element does not equal their userId, it will kick them. You need to do a final check after running through the entire table.

local whitelist = {
	"664880421",
	"2513627682",
}

game.Players.PlayerAdded:Connect(function(player)
  local whitelisted = false
  for i,v in pairs(whitelist) do
    if v == player.UserId then
      whitelisted = true
      break;
    end
  end
  if not whitelisted then
    player:Kick("You're not allowed here.")
  end
end)

I think I made some type of mistake while coding the original script cause this still doesn’t work.

are you sure the script is running? Is it in a server script??

what Farley120 said

Remove the quotation marks when storing the userIds, it’s storing them as a string.

Updated code:

local whitelist = {
	664880421,
	2513627682,
}

game.Players.PlayerAdded:Connect(function(player)
  local whitelisted = false
  for i,v in pairs(whitelist) do
    if v == player.UserId then
      whitelisted = true
      break;
    end
  end
  if not whitelisted then
    player:Kick("You're not allowed here.")
  end
end)

Do this instead

local players = game:GetService("Players")

local whitelist = {
	664880421,
	2513627682,
}

players.PlayerAdded:Connect(function(player)
	if not table.find(whitelist, player.UserId) then
		player:Kick("Not whitelisted")
	end
end)

A for loop is unnecessary for this

What this will do is check if someone who joins the game has a userid that is in the table. If they don’t have a userid that is in the table, they will get kicked from the game.


local whitelist = {664880421,2513627682}

game.Players.PlayerAdded:Connect(function(player)
      if not whitelist[player.UserId] then
             player:Kick("You're not allowed to join this game.")
      end
      -- do stuff down here for if they are allowed to join (in the whitelist)
end)