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

Help with Leaderboard Death?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

The value does not go up when I die.

--/LEADERBOARD

game.Players.PlayerAdded:connect(function(player)
    local Stats = Instance.new("Model", player)
    Stats.Name = "leaderstats"
    local WOs = Instance.new("IntValue", Stats)
    WOs.Name = "WOs"
    WOs.Value = 0

    local CharH = player.Character.Humanoid.Health
    if CharH == 0 then
        WOs.Value = WOs.Value +1
    end
end)
0
You are not constantly checking if the value is zero, you should use the humanoid Died() event CaptainRuno 40 — 8y

1 answer

Log in to vote
1
Answered by
Legojoker 345 Moderation Voter
8 years ago

Because if statements only check a conditional one time, it is sub-optimal to use it for this type of purpose, where you need a consistent checking method to make sure the person gets a point when they die. Humanoids can have a listening event attached to them called Died which is called when a humanoid's health hits 0. Since the humanoid is refreshed with a new character, one humanoid listening event connection simply won't do. We will need to also call a function for every time a character is added, which will lead to a humanoid being added. What we get is something similar to your code with a slight change at the end.

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new("Model", plr)
    stats.Name = "leaderstats"
    local WOs = Instance.new("IntValue", stats)
    WOs.Name = "WOs"
    WOs.Value = 0
    plr.CharacterAdded:connect(function(chr)
        chr:WaitForChild("Humanoid").Died:connect(function()
            if WOs and WOs.Parent then
                WOs.Value = WOs.Value+1
            end
        end)
    end)
end)

Hope this helped, and if it did, please don't forget to upvote!

0
Any other way instead of using "Died"? FiredDusk 1466 — 8y
Ad

Answer this question