New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Leaderboard Problem

Asked by 6 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 5436 Badge of Merit Moderation Voter Community Moderator
6 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 — 6y
0
The ends were necessary, I was just asking why they were all on a single line. :P adark 5436 — 6y
0
yeah, thats what I meant. I don't know why I put all those ends on one line User#348 0 — 6y
Ad

Answer this question