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

Can somebody please explain why cash is not paying out until player dies on leaderboard?

Asked by 6 years ago

Hello! I am trying to make a leaderboard for a game that computes kills, deaths & awards cash the longer a player is in the game.

It works great except the Cash does not begin paying out until the player dies.

I've moved it around and am still running into problems- Any suggestions?

Thanks.

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

    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent= plr 

    local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    kills.Parent = stats 

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = stats 

    local score = Instance.new("IntValue") 
    score.Name = "Cash" 
    score.Parent = stats
    score.Value = 1


    plr.CharacterAdded:Connect(function(char)

        local humanoid

        repeat 
            humanoid = char:FindFirstChild("Humanoid")
            wait()
        until humanoid

        humanoid.Died:Connect(function()

            deaths.Value = deaths.Value + 1

            local tag = humanoid:FindFirstChild("creator")

            if tag then

                local killer = tag.Value

                if killer then

                    killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1

                end
            end
        while true do
        wait(5)
        score.Value = score.Value + 3
            end
        end)
    end)
end)

1 answer

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

If you notice your ends

            end
        end)
    end)
end)

The while loop is inside of the Died event listener!

You should move the loop inside of the PlayerAdded event listener.

The code is not indented well, and this could be why it is hard to see the issue.

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

    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent= plr 

    local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    kills.Parent = stats 

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = stats 

    local cash = Instance.new("IntValue") -- # this is cash! not score
    cash.Name = "Cash" 
    cash.Parent = stats
    cash.Value = 1

    plr.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid") -- # Do not loop wait until something exists.

        humanoid.Died:Connect(function()
            deaths.Value = deaths.Value + 1
            local tag = humanoid:FindFirstChild("creator")

            if tag then
                local killer = tag.Value

                if killer then
                    killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
                end
            end
        end)
    end)

    while true do -- # in the PlayerAdded event listener
        wait(5)
        cash.Value = cash.Value + 3
    end
end)
0
Don't forget to hit "Accept Answer" if this helped you. User#24403 69 — 6y
0
Doh! I've been banging my head over this forever. THANK YOU!!!!!!!!!!!!!!!! bella_boopy 13 — 6y
Ad

Answer this question