Finding Value in Table

Is there a function that allows you to find a value in a table? For example, if I wanted to make a ban script,

-- (Pseudocode)

local banned = {plr1, plr2}

game.Players.PlayerAdded:Connect(function(plr)
    if plr.Name:IsInTable(banned) then
        plr:Kick("You are banned!")
    end
end)
table.find(banned, plr.Name)
2 Likes
local banned = {plr1, plr2}

game.Players.PlayerAdded:Connect(function(plr)
    if table.find(banned, plr.Name) then
        plr:Kick("You are banned!")
    end
end
1 Like

I wouldn’t advise banning by Player.Name, as players can change their username to evade the ban and continue playing on the same account. Player.UserId should be used instead. I wouldn’t personally even bother with pre-fetching a ban list into a table either, as this doesn’t scale well. Consider instead just using an OrderedDataStore. It doesn’t matter what you store, just store any integer you please with the key SetAsync(tostring(BannedPlayer.UserId),someInteger) and then just the existence of the key in the datastore is enough to know if the player is banned. In other words, if GetAsyc( tostring(BannedPlayer.UserId)) returns anything not nil, it’s a banned player.

I recommend OrderDataStore over normal DataStore so that in future you can easily get the list of banned players if you need it for some reason. There is no way to just get all the keys of a regular datastore.