How to detect if player isn't in a table?

It would print “Not whitelisted: Afraid4life.” and “Whitelisted: Afraid4life.” my name was on the list. If i remove my name from the list it would print “not Whitelisted: Afraid4life.” like it should be.

local whitelist = {} -- removed names
game.Players.PlayerAdded:Connect(function(player)
	repeat wait(2) until player
	for i,whitelisted in pairs(whitelist) do
		if whitelisted[player.Name]  then
			print("Whitelisted: "..player.Name..".")
			else
				print("Not whitelisted:"..player.Name..".")
			end
		end	

whitelisted
image
Not whitelisted
image

Hello, so you can use the same for loop to find the person inside of the table called whitelist. You can also use table.find(), but I prefer to use a for loop.

for i, whitelisted in pairs(whitelist) do
	if whitelisted == player.Name  then
		print(player.Name .. " is whitelisted!")
	else
		print(player.Name .. " is not whitelisted")
	end
end
2 Likes

How are you adding names to the whitelist? Can you show an example? The reason I’m asking is because you’re accessing the whitelist table as a hash of the player name, but that won’t work if the whitelist is just an array of names.

Also, repeat wait(2) until player does nothing but yield your program for 2 seconds. The player object exists immediately. I’d recommend just removing that line.

2 Likes
local whitelist = {"Afraid4Life", "Player1", "Player2"}

These aren’t the actual names I have in there, but this is how I added the names.

if not whitelisted[player.Name] then

Ok, then I suggest doing what @desinied mentioned. I would personally prefer table.find though.

local whitelist = {}
game.Players.PlayerAdded:Connect(function(player)
	if table.find(whitelist, player.Name) then
		print("Whitelisted: " .. player.Name .. ".")
	else
		print("Not whitelisted: " .. player.Name .. ".")
	end
end)
2 Likes

I’ve tried that before, it didn’t do anything.

If you want to use the hash method (e.g. whitelisted[player.Name]), then you would need to implement your whitelist differently as such:

local whitelist = {
	["PlayerName"] = true,
	["AnotherPlayer"] = true
}
game.Players.PlayerAdded:Connect(function(player)
	if whitelist[player.Name] then
		print("Whitelisted: " .. player.Name .. ".")
	else
		print("Not whitelisted: " .. player.Name .. ".")
	end
end)

Also, because player names can change, it’s better to check for the UserId of the player instead of the Name.

1 Like

I’m in a rush, so I’m really sorry if the indentation is off. I just go through the table and return true or false depending if the item is in it:

local function intbl(tbl, itm)

   for i, v in pairs(tbl) do
      if v == itm then
         return true
      end
   end
   return false
end