Timer Not resetting

Hello, I am working on a timer, but when the player dies and respawns the timer wont run again heres the code i am using , please let me know if you need more info :slight_smile:

PS sorry for the messy code

local timer

game.Players.PlayerAdded:Connect(function(player)
	timer = player.PlayerGui:WaitForChild("ScreenGui").TextLabel
	print(player.Parent)
	print(player)
end)



local mill = 00
local sec = 00
local min = 00

local start = false

local function timerStart()
	while start == true do 
		wait(0.0455)
		mill = mill + 5
		if mill >= 100 then
			mill = mill - 100
			sec = sec + 1
		end
		if sec >= 60 then
			sec = sec - 60
			min = min + 1 
		end
		if mill <= 9 then 
			timer.Text = min..":"..sec..":".."0"..mill
		end
		if sec <= 9 then 
			timer.Text = min..":".."0"..sec..":"..mill
		end
		if min <= 9 then 
			timer.Text = "0"..min..":"..sec..":"..mill
		end
		if mill <= 9 and sec <= 9 then
			timer.Text = min..":".."0"..sec..":".."0"..mill
		end
		if mill <= 9 and min <= 9 then
			timer.Text = "0"..min..":"..sec..":".."0"..mill
		end
		if min<= 9 and sec <= 9 then
			timer.Text = "0"..min..":".."0"..sec..":"..mill
		end
		if min <= 9 and sec <= 9 and mill <= 9 then
			timer.Text = "0"..min..":".."0"..sec..":".."0"..mill
		end
	end
end

workspace.Start1.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if timer.Text ~= "00:00:00" then
			mill = 00
			sec = 00
			min = 00
		end
		start = true
		timer.TextColor3 = Color3.new(0.172549, 0.92549, 0.262745)
		timer.TextTransparency = 0
		workspace.Start1.CanTouch = true
		
		timerStart()

		workspace.Start1.CanTouch = false
		workspace.Finish1.CanTouch = true
		wait(3)
		end
end)

workspace.Finish1.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") then
		timer.TextColor3 = Color3.new(1, 0.576471, 0.294118)
		start = false
		workspace.Start1.CanTouch = true
		workspace.Finish1.CanTouch = false
	end
	wait(3)
end)

workspace.Death.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") then
		timer.TextColor3 = Color3.new(1, 0.576471, 0.294118)
		start = false
		workspace.Start1.CanTouch = true
		workspace.Finish1.CanTouch = false
	end
	wait(3)
end)

for i, v in pairs(game.Players:GetPlayers()) do
      v.Character.Humanoid.Died:Connect(function()
local timer = v.PlayerGui.ScreenGui.TextLabel
wait(1)
timerStart()
     end
end
1 Like

I’m just gonna say that I hope you only have maximum of 1 player in the game as timer is a global variable in the script

nevertheless, a potential solution would be setting the screengui property resetguiondeath bool to false, this will prevent the gui from being destroyed and the instance will be the same.

1 Like

Needs to be wrapped inside a “CharacterAdded” event if you want the timer to restart each time the player’s character’s humanoid’s health reaches 0.

local timer

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		timer = player.PlayerGui.ScreenGui.TextLabel
	end)
end)