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

Leaderboard Problem

Asked by 10 years ago

Hello! I am trying to make a script that gives you a point when you are between 100 and -5 on the y axis. There is no error in the output, but I still dont get any points. The only scoreboard objectives are Points and High Score. Here is the code I am using.

while wait(2.1) do
if Workspace.Running.Value == true then
for _,Player in pairs(game.Players:GetPlayers()) do
        for i,v in pairs(Workspace:GetChildren()) do
            if v.Name == Player.Character then
                player = v      
        if Player:FindFirstChild("leaderstats") then
            leaderstats = Player.leaderstats
            points = leaderstats:FindFirstChild("Points")
            highscore = leaderstats:FindFirstChild("High Score")
            if player.Torso.Position.y < 100 and player.Torso.Position.y > -5 then
            points.Value = points.Value + 1
            if points.Value > highscore.Value then
            highscore.Value = points.Value
end end end end end end end end

Hopefully you can help!

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

Your problem is on line 5 of that code. Player.Character is an Instance and v.Name is a string, so that if statement will never be true.

As well, there's no reason to nest the loops like. It's more efficient to just access the Player's Character directly.

Here is your code, modified to work better:

while wait(2.1) do
    if Workspace.Running.Value == true then
        for _,Player in pairs(game.Players:GetPlayers()) do
            local player = Player.Character
            if player then
                leaderstats = Player:WaitForChild("leaderstats")
                points = leaderstats:WaitForChild("Points")
                highscore = leaderstats:WaitForChild("High Score")
                if player.Torso.Position.y < 100 and player.Torso.Position.y > -5 then
                    points.Value = points.Value + 1
                    if points.Value > highscore.Value then
                        highscore.Value = points.Value
                    end
                end
            end
        end
    end
end

Quick question, why do you work the ends into one line like that? It detracts from the code's readability.

0
I forgot that you could do that... :P Thanks though! And I don't really know why I did all those ends... User#348 0 — 10y
0
The ends were necessary, I was just asking why they were all on a single line. :P adark 5487 — 10y
0
yeah, thats what I meant. I don't know why I put all those ends on one line User#348 0 — 10y
Ad

Answer this question