Timer being set to -1:59

Right now I have a timer system for each induvial player. The timer system involves a GUI on the players screen.

When the player dies from the line Player.Character.Humanoid.Health = 0, their local timer is set to -1:59. I just am unsure on why it is being set to that value. I would appreciate some help, here is a video on what is happening.

Once the player touches a part, a remote event will fire and trigger a timer (I didn’t show this in the video). But once the player dies, the timer is set to -1:59 when instead it should just reset.

Here is the timer code which is a local script in StarterPlayerScripts:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("OneMinuteTimer")
local remoteEvent2 = ReplicatedStorage:WaitForChild("EndTimer")
local Checkpoint = workspace:WaitForChild("Checkpoints")

local lastCheckpointTouched = nil

local function check()
	local currentCheckpointName = lastCheckpointTouched.Name
	local nextCheckpointName = tostring(tonumber(currentCheckpointName) + 1)
	local nextCheckpoint = game.Workspace.Checkpoints:FindFirstChild(nextCheckpointName)

	if nextCheckpoint then
		Player.Character:MoveTo(nextCheckpoint.Position + Vector3.new(0, 1, 0))
	end
end

local function remove()
	for _, tool in ipairs(Player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			tool:Destroy()
		end
	end
	if Player.Character:FindFirstChildOfClass("Tool") then
		Player.Character:FindFirstChildOfClass("Tool"):Destroy()
	end
end

local clonedgui = Player.PlayerGui:WaitForChild("Timer")
local minutes, seconds = 0, 0
Started = false
local mode = false


local red = Color3.fromRGB(231, 101, 101)
local red2 = Color3.fromRGB(126, 69, 69)
local white = Color3.fromRGB(255,255,255)
local green1 = Color3.fromRGB(190, 231, 178)
local green2 = Color3.fromRGB(127, 198, 89)

local timersound = game.ReplicatedStorage.TimerSound

function StartTimer()
	while minutes >= 0 do
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
		else 
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
		end
		if minutes <= 0 and seconds <= 10 and seconds >= 9.5 then
			clonedgui.Frame.TextLabel.TextColor3 = red
			clonedgui.Frame.TextLabel.UIStroke.Color = red2
			timersound:Play()
		end
		if minutes <= 0 and seconds <= 0 then
			if mode == false then
				print(mode)
				remove()
				check()
			else
				print(mode)
				Player.Character.Humanoid.Health = 0
			end
		end
		wait(1)
		Player.CharacterAdded:Connect(ResetTimer)
	end 
end

function ResetTimer()
	if not Started then
		Started = true
		task.spawn(StartTimer)
	else
		minutes, seconds = minutes, seconds
	end
end

remoteEvent.OnClientEvent:Connect(function(checkpointPart)
	if checkpointPart and checkpointPart:IsA("BasePart") then
		if lastCheckpointTouched ~= checkpointPart then
			lastCheckpointTouched = checkpointPart

			print(lastCheckpointTouched.TimeValue.Value)

			if lastCheckpointTouched.TimeValue.Value == 1 then
				minutes, seconds = 1, 0
			elseif lastCheckpointTouched.TimeValue.Value == 2 then
				minutes, seconds = 0, 45
			elseif lastCheckpointTouched.TimeValue.Value == 3 then
				minutes, seconds = 0, 30
			elseif lastCheckpointTouched.TimeValue.Value == 0 then
				minutes, seconds = 0, 0
			else
				print("Hi - Unknown Checkpoint Value")
			end

			if lastCheckpointTouched.Mode.Value == true then
				mode = true
			else
				mode = false
			end

			if clonedgui.Frame.TextLabel.TextColor3 ==  red and clonedgui.Frame.TextLabel.UIStroke.Color == red2 then
				clonedgui.Frame.TextLabel.TextColor3 = green1
				clonedgui.Frame.TextLabel.UIStroke.Color = green2
			end

			ResetTimer()
		end
	end
end)

I also should mention that the part the player steps on to activate the timer has a BoolValue called Mode and a IntValue called TimeValue parented to it.

3 Likes

This should stop at 0:00 :slight_smile:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("OneMinuteTimer")
local remoteEvent2 = ReplicatedStorage:WaitForChild("EndTimer")
local Checkpoint = workspace:WaitForChild("Checkpoints")

local lastCheckpointTouched = nil

local function check()
	local currentCheckpointName = lastCheckpointTouched.Name
	local nextCheckpointName = tostring(tonumber(currentCheckpointName) + 1)
	local nextCheckpoint = game.Workspace.Checkpoints:FindFirstChild(nextCheckpointName)

	if nextCheckpoint then
		Player.Character:MoveTo(nextCheckpoint.Position + Vector3.new(0, 1, 0))
	end
end

local function remove()
	for _, tool in ipairs(Player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			tool:Destroy()
		end
	end
	if Player.Character:FindFirstChildOfClass("Tool") then
		Player.Character:FindFirstChildOfClass("Tool"):Destroy()
	end
end

local clonedgui = Player.PlayerGui:WaitForChild("Timer")
local minutes, seconds = 0, 0
local timerRunning = false
local mode = false

local red = Color3.fromRGB(231, 101, 101)
local red2 = Color3.fromRGB(126, 69, 69)
local white = Color3.fromRGB(255, 255, 255)
local green1 = Color3.fromRGB(190, 231, 178)
local green2 = Color3.fromRGB(127, 198, 89)

local timersound = game.ReplicatedStorage.TimerSound

local function StartTimer()
	timerRunning = true
	while minutes >= 0 do
		if seconds <= 0 then
			if minutes <= 0 then
				break
			end
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
		else 
			clonedgui.Frame.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
		end
		if minutes <= 0 and seconds <= 10 and seconds >= 9.5 then
			clonedgui.Frame.TextLabel.TextColor3 = red
			clonedgui.Frame.TextLabel.UIStroke.Color = red2
			timersound:Play()
		end
		if minutes <= 0 and seconds <= 0 then
			if mode == false then
				remove()
				check()
			else
				Player.Character.Humanoid.Health = 0
			end
		end
		wait(1)
	end
	
	-- Update the TextLabel once the timer finishes
	clonedgui.Frame.TextLabel.Text = "0:00"
	clonedgui.Frame.TextLabel.TextColor3 = green1
	clonedgui.Frame.TextLabel.UIStroke.Color = green2
	
	timerRunning = false
end

local function ResetTimer()
	if not timerRunning then
		minutes, seconds = 0, 0
		StartTimer()
	end
end

remoteEvent.OnClientEvent:Connect(function(checkpointPart)
	if checkpointPart and checkpointPart:IsA("BasePart") then
		if lastCheckpointTouched ~= checkpointPart then
			lastCheckpointTouched = checkpointPart

			if lastCheckpointTouched.TimeValue.Value == 1 then
				minutes, seconds = 1, 0
			elseif lastCheckpointTouched.TimeValue.Value == 2 then
				minutes, seconds = 0, 45
			elseif lastCheckpointTouched.TimeValue.Value == 3 then
				minutes, seconds = 0, 30
			elseif lastCheckpointTouched.TimeValue.Value == 0 then
				minutes, seconds = 0, 0
			else
				print("Hi - Unknown Checkpoint Value")
			end

			if lastCheckpointTouched.Mode.Value == true then
				mode = true
			else
				mode = false
			end

			if clonedgui.Frame.TextLabel.TextColor3 == red and clonedgui.Frame.TextLabel.UIStroke.Color == red2 then
				clonedgui.Frame.TextLabel.TextColor3 = green1
				clonedgui.Frame.TextLabel.UIStroke.Color = green2
			end

			ResetTimer()
		end
	end
end)

2 Likes

Thanks for the response. The timer never activated once I stepped on the part. The GUI just stayed at 0:00.

I am assuming you are new to scripting because normally you would want to put a “local” before you put this

and you also made a mistake here when you put tostring

it will not check it as a number but as text when it does this so you just need to change this to a tonumber() or it will error. Also this function right here

is never going to be called by this

because it is above instead of below the function. There is also this

which isn’t necessarily a bug but it is more recommend to do

while minutes > 0 do
if seconds > 0 then -- the code
1 Like

Yes I am some what new to programming. I added what you suggested but it still did not end up fixing the timer problem.

2 Likes

I’d suggest using a single variable for the time, and have it be in seconds.

local timerTime = 2*60

You can count down the timer similar to how you were doing it before.

wait(1)
timerTime = timerTime -1

You can make sure it never goes below 0 by using math functions

timerTime = math.max(0, timerTime)

You can convert it to timer text like this:

local minutes = math.floor(timerTime/60)
local text = string.format("%i:%02i", minutes, timerTime%60)
textLabel.Text = text

Your functionality when the timer reaches zero should be the same as before

if timerTime <= 0 then
	-- die!
end
1 Like

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