How would I check if a single value is equal to any of the values in a table?

I am currently in a testing phase for a game I created myself, and I’ve hit a small block in understanding how to restrict certain players.

As of now, I’m trying to compose code in which the server kicks any users whose UserId is NOT equal to any of the stored ID’s in an array in server script.

I am, in a nutshell, trying to make a whitelisted “tester” sort of system for now.

I will provide some improvisation I have come up with:

local WhitelistedUsers = {
665958081,
1390754809 -- For test reasons right now, more players to be added
}

function CheckPlayerAuth(Player)
local UserId = Player.UserId

if not WhitelistedUsers[UserId] then

Player:Kick("You are not a recognized game tester.)

end


game.Players.PlayerAdded:Connect(CheckPlayerAuth)

I am terribly sorry for the format not being very organized - I am in a rush currently… D:

4 Likes

This should work.

local WhitelistedUsers = {665958081,1390754809}

function CheckPlayerAuth(Player)
local UserId = Player.UserId

if table.find(WhitelistedUsers, UserId) then
print("Player allowed")
else
Player:Kick("You are not a recognized game tester.")

end
end

game.Players.PlayerAdded:Connect(CheckPlayerAuth)
6 Likes

You can iterate over the table and check if any of the elements are equal to the one you’re looking for, which is the same as what occurs in the table.find function added by Roblox, or you can make another table which is a dictionary where the keys are the values from the original table, e.g.

local keys = {[665958081] = true, [1390754809] = true}

if not keys[UserId] then
	...
end
2 Likes

Thank you both very much! I tried Ignacasas06’s solution but I will definitely be looking into the iteration method, @1waffle1! :smiley:

For something like this, you could just set the permissions in game settings instead of having to write a script like this.