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

How can I make my player's 'time alive' leaderstat reset when they die?

I've seen things like this before but they've all been no use. Here is my code, can someone help me reset the 'Time alive' counter when the player dies? if you need a better understanding of why I'll need this, here's the link to my game:

https://web.roblox.com/games/4959857494/Dash-BETA

Here is my script:

game.Players.PlayerAdded:connect(function(player)
 local Stats = Instance.new("Model", player)
 Stats.Name = "leaderstats"
 local Timer = Instance.new("IntValue", Stats)
 Timer.Name = "Time Alive"
 Timer.Value = 0
 while true do
  wait(1)
  Timer.Value = Timer.Value +1
 end
 
end)

1 comment

4 answers

✓ Accepted answer

Make it sense if the health is 0, using if player.Character.Humanoid.Health == 0 then, like this:

game.Players.PlayerAdded:connect(function(player)
 local Stats = Instance.new("Model", player)
 Stats.Name = "leaderstats"
 local Timer = Instance.new("IntValue", Stats)
 Timer.Name = "Time Alive"
 Timer.Value = 0
 while true do
  wait(1)
  if player.Character.Humanoid.Health == 0 then
	Timer.Value = 0
	print("Player died")
  end
  Timer.Value = Timer.Value +1
 end

end)

3 comments

0

why a down vote ?

0

There is no use in constantly checking if the player's health is at 0, you can simply use the Died() event.

0

thats what i thought earlier, but a hooked function may not work correctly under an infinite loop

Here's it:

game.Players.PlayerAdded:connect(function(player)
 local Stats = Instance.new("Model", player)
 Stats.Name = "leaderstats"
 local Timer = Instance.new("IntValue", Stats)
 Timer.Name = "Time Alive"
 Timer.Value = 0
local Rank = Instance.new("StringValue")
Rank.Parent = Stats
local ID = 1331531 --Switch this ID with your group's ID.
Rank.Value = player:GetRoleInGroup(ID)
Rank.Name = "Rank"
 while true do
  wait(1)
  Timer.Value = Timer.Value +1
 end

end)

1 comment

0

how do i use it

Here's the script you can use to accomplish this:

game.Players.PlayerAdded:Connect(function(player)
	
	local Stats = Instance.new("Model", player)
	Stats.Name = "leaderstats"
	
	local Timer = Instance.new("IntValue", Stats)
	Timer.Name = "Time Alive"
	
	while true do
 		wait(1)
		Timer.Value = Timer.Value +1
	end
	
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			Timer.Value = 0
		end)
	end)
	
end)

I added some indentation to help with readability so it's easier for you and others to understand. Also, I removed the line where you set "Timer's" value to 0 because it already defaults at 0. All that's happening is that it detects when the player's character loads in, then checks for when the character's Humanoid dies. When the Humanoid dies, it resets the value to 0.

Hope this helps.

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