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

No value in a leaderboard?

Asked by 9 years ago
Admins = {"groovydino","Player1"}

game.Players.PlayerAdded:connect(function(plr)

    for i,v in pairs(Admins) do
        if v == plr.Name then

    plr.leaderstats["Admin?"].Value = "Admin"
        else
            plr.leaderstats["Admin?"].Value = "Non-Admin"
        end
    end
end)

The problem is that I have an admin leaderboard, saying whether you are an admin or not, except the value is blank. I think the problem is that the script runs before the player loads. I'm not sure.

1
What is the error, if its what I think change the Value for "Admin?" fo a StringValue XToonLinkX123 580 — 9y
0
Dude, it IS a string value. groovydino 2 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

I assume you have another script responsible for creating the "Admin?" value. It would be better to merge these two scripts; this script is probably running before the other one completes, which would cause an "attempt to index nil" error.

Don't forget that you can look at the Output window when testing (or, if online, press F9 for developer console) to find error messages and script output.

Your script also has a problem: you iterate over each Admin name for the player and reassign the Admin? value each time. Even if someone is an admin, they may be labelled "Non-Admin". To fix, use a "found" variable (or "isAdmin") that starts out "false" and becomes "true" if you find their name in the list:

Admins = {"groovydino","Player1"}

game.Players.PlayerAdded:connect(function(plr)
    local isAdmin = false
    for i,v in pairs(Admins) do
            if v == plr.Name then
            isAdmin = true
            break
        end
    end
    if isAdmin then
        plr.leaderstats["Admin?"].Value = "Admin"
    else
        plr.leaderstats["Admin?"].Value = "Non-Admin"
    end
end)

Although it doesn't matter for such an infrequent event, if you're going to have more than 1 or 2 admins, you can improve the efficiency of the script by using a dictionary instead of a list:

Admins = {"groovydino","Player1"}
for i = 1, #Admins do --convert list to dictionary
    Admins[Admins[i]] = true
    Admins[i] = nil
end

game.Players.PlayerAdded:connect(function(plr)
    plr.leaderstats["Admin?"].Value = Admins[plr.Name] and "Admin" or "Non-Admin"
end)

Don't forget that you either need to merge the scripts, or at least add a "WaitForChild" before accessing the "Admin?" value, ex: plr.leaderstats:WaitForChild("Admin?").Value

Ad

Answer this question