How do I make this loop until someone dies, then reset the timer?

Hey! So as the title says, I can’t quite figure out how to create a time alive counter until you die which will then reset to 0. This is what I have so far



local canGet = true


	
game.Players.PlayerAdded:connect(function(player)
	
		if player and canGet then

			canGet = false

		player.leaderstats.Timealive.Value = player.leaderstats.Timealive.Value + 1 

			wait(1)

			canGet = true

		end
	end)


I have tried the while true loop, although that doesn’t seem to work. The script runs once and then just ends, giving the player one point. I also already created the leaderboard in another script.

Any help?

Is this what You are looking for?

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local canAdd = true
		character:WaitForChild("Humanoid").Died:Connect(function()
			canAdd = false
			player.leaderstats.Timealive.Value = 0
		end)
		while canAdd and task.wait(1) do
			player.leaderstats.Timealive.Value += 1
		end
	end)
end)

Let me know if You don’t understand something in my code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.