Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Death counter doesn't increase properly? [SOLVED]

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

✓ Accepted answer

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 :)

8 comments

0

Thank you very much, it did! :)

0

If this helped, accept answer or put in title [SOLVED] to mark as resolved

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).

0

Instance.new("instance",parent) is deprecated: https://wiki.roblox.com/index.php?title=Instance_(Data_Structure)

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.

0

IE -- Always do: .Text = "TEXT" ; .Parent = Parent over .Parent = Parent ; .Text = "TEXT"

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.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.