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

Death counter doesn't increase properly? [SOLVED]

Asked by 5 years ago
Edited 5 years ago

I have a game where I want to show the number of deaths a player has in the leaderboard, and I've made this for it in ServerScriptService:

game.Players.ChildAdded:connect(function(player)
    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = player.leaderstats

    player.Changed:connect(function()
        repeat wait() until player.Character
        player.Character.Humanoid.Died:connect(function()
            deaths.Value = deaths.Value + 1
        end)
    end)
end)

However, instead of increasing by one on the first death, it increases by four. But deaths after that increase it normally.

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

For first, use game.Players.PlayerAdded not game.Players.ChildAdded and use player.CharacterAdded not player.Changed

remember :connect is deprecated. use :Connect

Example:

game.Players.PlayerAdded:Connect(function(player) -- Detect player join
    print("Player added")
    player.CharacterAdded:connect(function(char) -- Detect char spawned
        print("Character added")
        char.Humanoid.Died:Connect(function() -- Detect humano died
            print("Human died")
        end)
    end)
end)

Here is fixed script:

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

    -- Detect for leaderstats

    local leaderstats

    if player:FindFirstChild("leaderstats") then
        leaderstats = player.leaderstats
    else
        leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = player
    end

    -- create deaths value

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

    -- detect character added.

    player.CharacterAdded:Connect(function(char)
    -- detect humanoid died.
        char.Humanoid.Died:Connect(function()
            deaths.Value = deaths.Value + 1 -- add 1 value to values
        end)
    end)
end)

Wiki pages:

CharacterAdded

PlayerAdded

Humanoid.Died

Hope it helped :)

0
Thank you very much, it did! :) HomerJC 2 — 5y
0
If this helped, accept answer or put in title [SOLVED] to mark as resolved yHasteeD 1819 — 5y
0
Good answer - also, always set the Parent property last for efficiency purposes. It's conceptually the same as why you shouldn't use Instance.new("instanceOfClass", Parent) (line 11-12). SummerEquinox 643 — 5y
0
Instance.new("instance",parent) is deprecated: https://wiki.roblox.com/index.php?title=Instance_(Data_Structure) yHasteeD 1819 — 5y
View all comments (4 more)
0
I didn't say it wasn't. I was letting you know that efficiency-wise you should be setting Parent after you have set all other properties. SummerEquinox 643 — 5y
0
IE -- Always do: .Text = "TEXT" ; .Parent = Parent over .Parent = Parent ; .Text = "TEXT" SummerEquinox 643 — 5y
0
Ah now i understood. I didn't quite understand what you said because my main language is not English. I understand very little, Thanks for warn this. yHasteeD 1819 — 5y
0
No worries, here is a full thread if you're interested: https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296 SummerEquinox 643 — 5y
Ad

Answer this question