Question

I made a little game and I want to check if a player name already exists in a "banlist". How can I do this if it's multiple names? For example I have a playerlist like this:

 PlayerList = {'Player 1', 'Player 2', 'Player 3'}

And I want to be able to ban a few players and add them to a ban list to forbid them from doing some stuff in the game. How can I do that? I can do it with 1 playername like this:

if (Player_Name ~= 'Player 2') then
print('Hello!!')
else
print('You are banned!')
end

But this would only work for "Player 2". How can I add multiple names? I do not want to use "or", like this:

if (Player_Name ~= 'Player 2' or Player_Name ~= 'Player 3') then
print('Hello!!')
else
print('You are banned!')
end

Since my list might contain more than 200, I don't want to add over 200 "or". How can I simply check if the player is in a banlist I created? For example:

BanList = {'Player 2', 'Player 3'}

Maybe something like this (ofc this doesnt work)

if (Player_Name ~= BanList) then
print('Hello!!')
else
print('You are banned!')
end
Was it helpful?

Solution

You want to use a loop to iterate through all the items in your BanList and see if any of them contain the player.

BanList = { 'Player 2', 'Player 3' }

function isBanned(playerName)
    for _,name in pairs(BanList) do
        if name == playerName then
            return true
        end
    end
    return false
end

if isBanned(Player_Name) then
    print('You are banned!')
else
    print('Hello!!')
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top