FInding Value in Table

So I have a script basically for a somewhat a whitelist system; if your user id is found in it then it sets a variable (local val = true) and if not found then kicks you.

The thing is when I do this;

for i, v in pairs(whitelisteduserids) do
   if v == userid then
      val = true
      print("authorized")
      break
   elseif v ~= userid then
      val = false
      player:Kick()
      break
   end
end

The issue is like, no matter what even if the User ID is indeed in the whitelist table it will still kick them, so how do I prevent it? Is it breaking the loop that’s the problem or what ?

Most likely the issue is that you shouldn’t kick the player immediately when v ~= userid, because it might not be the first one on the list. Try kicking them after you check all the UserIds. Here’s an example of what I mean.

for i, v in pairs(whitelisteduserids) do
	if v == userid then
		val = true
		print("authorized")
		break
	elseif v ~= userid then
		val = false
	end
end

if val == false then
	print("unauthorized")
	player:Kick()
end
1 Like

Just use what @J_Angry said but maybe he forgot to add 1 thing to the script, anyway here’s it:

for i, v in pairs(whitelisteduserids) do
	if v == userid then
		val = true
		print("authorized")
		break
	elseif v ~= userid then
		val = false
        break
	end
end

if val == false then
	print("unauthorized")
	player:Kick()
end
1 Like

Wouldn’t that still not work because say it’s the 3rd on the list but there’s more, it’d just swap it back to false right or no?

It should still work, because the break item is supposed to stop the for loop in theory, meaning it should still be true.

1 Like

Alright, thank you again. I’ll let you know if it works.

Assuming you have an array of userIds, you can simply use table.find to search whether the player is whitelisted or not:

if table.find(whitelisteduserids, Player.UserId) then
    print("Authorized")
else
    Player:Kick()
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.