Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how would u use a table to check a players name?

Asked by 5 years ago

is there anyway to use a table to check a players name like this:

local players = {"paper", "someone", "aLightBulb"}

game.Players.PlayerAdded:Connect(function(player)
    if player.Name == players then
        print("yes")
    end
end)

but that wouldnt work, is there anyway to do this using a table like an array or something

1 answer

Log in to vote
1
Answered by
Vulkarin 581 Moderation Voter
5 years ago

Iteration of an array is very easy:

local players = {"paper", "someone", "aLightBulb"}
for _, player in pairs(players) do
    print(player) -- prints out "paper", "someone", "aLightBulb"
end

Now you merely check if the player name matches any of the table's values:

local players = {"paper", "someone", "aLightBulb", "Vulkarin"}

game.Players.PlayerAdded:Connect(function(player)
    local found = false
    for _, storedPlayer in pairs(players) do
        if player.Name == storedPlayer then
            print("yes")
            found = true
            break
        end
    end

    if not found then print("no") return end
end)
Ad

Answer this question