Why does the textlabel go back to "Label" instead of continuing when the player dies?

I’m trying to make it so when the player goes on stage 1, the timer starts, and ends on stage 2 which will be changed later. That part works perfectly; however, I’m trying to make it so even if they die, the timer will continue until they’ve reached the marked point. My script isn’t working though.

Local Script in Text Label

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local leaderstatChangedEvent = ReplicatedStorage:WaitForChild("LeaderstatChanged")

local textLabel = script.Parent 

local timer = 0
local running = false

local function UpdateTimer()
	while running do
		wait(0.01)
		timer = timer + 0.01

		local totalSeconds = math.floor(timer)
		local milliseconds = math.floor((timer % 1) * 100)
		local hours = math.floor(totalSeconds / 3600)
		local minutes = math.floor((totalSeconds % 3600) / 60)
		local seconds = totalSeconds % 60

		textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
	end
end

leaderstatChangedEvent.OnClientEvent:Connect(function(newValue)
	if newValue == 1 and not running then
		running = true
		UpdateTimer()
	elseif newValue == 2 then
		running = false
	end
end)

Regular Script in ServerScriptService

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local leaderstatChangedEvent = ReplicatedStorage:FindFirstChild("LeaderstatChanged")

local function onLeaderstatChanged(player, newValue)
	leaderstatChangedEvent:FireClient(player, newValue)
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local leaderstats = player:WaitForChild("leaderstats")
		local stat = leaderstats:WaitForChild("Stage")

		stat.Changed:Connect(function(newValue)
			onLeaderstatChanged(player, newValue)
		end)
	end)
end)

2 Likes

have the timer be a value stored in the player and get it and then add +.01, like make a numbervalue that is stored in the player

1 Like

Not working. Plus, the timer [numbervalue] keeps getting added when the player dies.

Updated Code [Local Script]:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local leaderstatChangedEvent = ReplicatedStorage:WaitForChild("LeaderstatChanged")

local textLabel = script.Parent


local timerValue = Instance.new("NumberValue")
timerValue.Name = "Timer"
timerValue.Value = 0
timerValue.Parent = player

local running = false

local function UpdateTimer()
	while running do
		wait(0.01)
		timerValue.Value = timerValue.Value + 0.01

		local totalSeconds = math.floor(timerValue.Value)
		local milliseconds = math.floor((timerValue.Value % 1) * 100)
		local hours = math.floor(totalSeconds / 3600)
		local minutes = math.floor((totalSeconds % 3600) / 60)
		local seconds = totalSeconds % 60

		textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
	end
end

leaderstatChangedEvent.OnClientEvent:Connect(function(newValue)
	if newValue == 1 and not running then
		running = true
		UpdateTimer()
	elseif newValue == 2 then
		running = false
	end
end)

1 Like

why did you crate the number value ion the local script? Also when the player dies did you want the timer to keep on adding?

I want it so when the individual dies, the timer continues going from where it left off from.

Let me get this staight, when the player dies the timer stops, when they respawn it keeps on going?

Yes, until the individual reaches the marked stage number, in this case 2.

does newvalue get changed when the player dies?

leaderstatChangedEvent.OnClientEvent:Connect(function(newValue)
	if newValue == 1 and not running then
		running = true
		UpdateTimer()
	elseif newValue == 2 then
		running = false
	end
end)

As far as I’m aware, no. It’s connected to the stage leaderstats, and the leaderstats don’t change.

instead of newvalue make a bool and when the player dies it is set to false and when the player it is alive then make it set to true

I added it to my script, now the timer won’t even start.

can i see the updated code?


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local textLabel = script.Parent


local timerValue = Instance.new("NumberValue")
timerValue.Name = "Timer"
timerValue.Value = 0
timerValue.Parent = player

local aliveStatus = Instance.new("BoolValue")
aliveStatus.Name = "IsAlive"
aliveStatus.Value = true
aliveStatus.Parent = player

local running = false

local function UpdateTimer()
	while running do
		wait(0.01)
		timerValue.Value = timerValue.Value + 0.01

		local totalSeconds = math.floor(timerValue.Value)
		local milliseconds = math.floor((timerValue.Value % 1) * 100)
		local hours = math.floor(totalSeconds / 3600)
		local minutes = math.floor((totalSeconds % 3600) / 60)
		local seconds = totalSeconds % 60

		textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
	end
end

local function OnCharacterAdded(character)
	aliveStatus.Value = true
end

local function OnCharacterRemoving(character)
	aliveStatus.Value = false
end

local function AliveStatusChanged()
	if aliveStatus.Value and not running then
		running = true
		UpdateTimer()
	elseif not aliveStatus.Value then
		running = false
	end
end

player.CharacterAdded:Connect(OnCharacterAdded)
player.CharacterRemoving:Connect(OnCharacterRemoving)

aliveStatus:GetPropertyChangedSignal("Value"):Connect(AliveStatusChanged)

if player.Character then
	OnCharacterAdded(player.Character)
else
	aliveStatus.Value = false
end
  1. remove allivestatus changed and make it so while allivestatus.value = true do
  2. You call UpdateTimer() which is in alivestatuschanged() whichj does not get called

I’m not sure what you mean completely but use your old code and just turn resetonspawn to false for the screengui.

I made it so “while aliveSTatus.Value == true do”, and I called the updatetimer function.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local textLabel = script.Parent

local timerValue = Instance.new("NumberValue")
timerValue.Name = "Timer"
timerValue.Value = 0
timerValue.Parent = player

local aliveStatus = Instance.new("BoolValue")
aliveStatus.Name = "IsAlive"
aliveStatus.Value = true
aliveStatus.Parent = player

local running = false

local function UpdateTimer()
	while aliveStatus.Value == true do
		wait(0.01)
		timerValue.Value = timerValue.Value + 0.01

		local totalSeconds = math.floor(timerValue.Value)
		local milliseconds = math.floor((timerValue.Value % 1) * 100)
		local hours = math.floor(totalSeconds / 3600)
		local minutes = math.floor((totalSeconds % 3600) / 60)
		local seconds = totalSeconds % 60

		textLabel.Text = string.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds)
	end
end

local function OnCharacterAdded(character)
	aliveStatus.Value = true
	UpdateTimer()
end

local function OnCharacterRemoving(character)
	aliveStatus.Value = false
end

player.CharacterAdded:Connect(OnCharacterAdded)
player.CharacterRemoving:Connect(OnCharacterRemoving)

if player.Character then
	OnCharacterAdded(player.Character)
else
	aliveStatus.Value = false
end

Oh my gosh, how didn’t I think of that. Thanks!

Yeah you see work smarter not harder, that’s how I roll. I write systems which are short but no one else has the brains like me. Yeahhhh i’m bragging what are yall gonna do abt it?

2 Likes

im not gonna stop you keep doing you rthing

My man my man mr monkey. my man.