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

Help with Leaderboard Death?

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)

1 comment

0

You are not constantly checking if the value is zero, you should use the humanoid Died() event

1 answer

✓ Accepted answer

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!

1 comment

0

Any other way instead of using "Died"?

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