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:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | local Stats = Instance.new( "Model" , player) |
03 | Stats.Name = "leaderstats" |
04 | local Timer = Instance.new( "IntValue" , Stats) |
05 | Timer.Name = "Time Alive" |
06 | Timer.Value = 0 |
07 | while true do |
08 | wait( 1 ) |
09 | Timer.Value = Timer.Value + 1 |
10 | end |
11 |
12 | end ) |
Make it sense if the health is 0, using if player.Character.Humanoid.Health == 0 then
, like this:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | local Stats = Instance.new( "Model" , player) |
03 | Stats.Name = "leaderstats" |
04 | local Timer = Instance.new( "IntValue" , Stats) |
05 | Timer.Name = "Time Alive" |
06 | Timer.Value = 0 |
07 | while true do |
08 | wait( 1 ) |
09 | if player.Character.Humanoid.Health = = 0 then |
10 | Timer.Value = 0 |
11 | print ( "Player died" ) |
12 | end |
13 | Timer.Value = Timer.Value + 1 |
14 | end |
15 |
16 | end ) |
Here's it:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | local Stats = Instance.new( "Model" , player) |
03 | Stats.Name = "leaderstats" |
04 | local Timer = Instance.new( "IntValue" , Stats) |
05 | Timer.Name = "Time Alive" |
06 | Timer.Value = 0 |
07 | local Rank = Instance.new( "StringValue" ) |
08 | Rank.Parent = Stats |
09 | local ID = 1331531 --Switch this ID with your group's ID. |
10 | Rank.Value = player:GetRoleInGroup(ID) |
11 | Rank.Name = "Rank" |
12 | while true do |
13 | wait( 1 ) |
14 | Timer.Value = Timer.Value + 1 |
15 | end |
16 |
17 | end ) |
Here's the script you can use to accomplish this:
01 | game.Players.PlayerAdded:Connect( function (player) |
02 |
03 | local Stats = Instance.new( "Model" , player) |
04 | Stats.Name = "leaderstats" |
05 |
06 | local Timer = Instance.new( "IntValue" , Stats) |
07 | Timer.Name = "Time Alive" |
08 |
09 | while true do |
10 | wait( 1 ) |
11 | Timer.Value = Timer.Value + 1 |
12 | end |
13 |
14 | player.CharacterAdded:Connect( function (character) |
15 | character.Humanoid.Died:Connect( function () |
16 | Timer.Value = 0 |
17 | end ) |
18 | end ) |
19 |
20 | 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.