Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't this admin verification script work?

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.