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

Why doesn't this admin verification script work?

Asked by 5 years ago

I'm trying to make a script when a player joins the game it would check if the player is an admin or not. The problem is that I put my name inside the admin table and it would still print out that I am not an admin (false). I do not know what is wrong with the code. If anyone could help it would be appreciated.

Script

local admins = {"jatytech", "Player1", "Player2", "Player3"}

local isAdmin = false

game.Players.PlayerAdded:Connect(function(player)
    for i,v in pairs(admins) do
        if player.Name == v then
            isAdmin = true
            break
        elseif player.Name ~= v then
            isAdmin = false
        end
    end
end)

print(isAdmin)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

It's because print(isAdmin) runs before the PlayerAdded event is fired by the addition of a player (the server skips events and comes back only when they are fired, and since serverscripts run before any player joins, the code after PlayerAdded runs).

To fix this, you can do:

local admins = {"jatytech", "Player1", "Player2", "Player3"}

local isAdmin = false

game.Players.PlayerAdded:Connect(function(player)
    for i,v in pairs(admins) do
        if player.Name == v then
            isAdmin = true
        print(isAdmin)
            break
        elseif player.Name ~= v then
            isAdmin = false
        print(isAdmin)
        end
    end
end)
Ad

Answer this question