Troubles with timer

I am trying to make a timer that can pause and reset.

However, nothing seems to really be working…
https://gyazo.com/1f8966ccc40a6bad26b80cbe799838b6

I have been tring to look in the developer forum for similar issues, however I have not had any luck.

Code:

local Time = script.Parent.TimeFrame:WaitForChild("Uptime")
local Play = script.Parent.TimeFrame:WaitForChild("Play")
local Reset = script.Parent.TimeFrame:WaitForChild("Reset")
local Pause =  script.Parent.TimeFrame:WaitForChild("Pause")

local CountUpToTime = 0

local TimerActive = false

local function PlayTimer()
	local Start = tick() 

	while true do
		wait()

		if TimerActive == false then

			CountUpToTime = CountUpToTime + 1

			local Difference = tick() - Start

			local Minutes = math.floor(Difference / 60)
			local Seconds = Difference - Minutes * 60
			local Milliseconds = math.floor((Seconds - math.floor(Seconds)) * 10) 
			Seconds = math.floor(Seconds)

			if string.len(Milliseconds) < 3 then Milliseconds = "0" .. Milliseconds end 
			if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
			if string.len(Minutes) < 2 then Minutes = "0" .. Minutes end
			--if string.len(Hours) < 2 then Hours = "0" .. Hours end

			local Format =  Minutes .. ":" .. Seconds .. ":" .. Milliseconds

			Time.Text = Format
		end
	end
end

local Create = coroutine.create(PlayTimer)

local function PauseTimer()
	TimerActive = true
	coroutine.yield(PlayTimer)
end

local function ResetTimer()
	TimerActive = true
	Time.Text = "00:00:00"
	coroutine.yield(PlayTimer)
end

Reset.MouseButton1Click:Connect(function()
	ResetTimer()
end)

Play.MouseButton1Click:Connect(function()
	TimerActive = false
	if TimerActive == false then 
		coroutine.resume(Create)
	end
end)

Pause.MouseButton1Click:Connect(function()
	PauseTimer()
	TimerActive = false
end)

If you know what I could fix, that would be great.
(Note: Never used coroutines before).

1 Like

From the gyazo, it seems like the time is not being reset after you click reset. It only changes the text but the actual time comes back after running again.

1 Like

Yes, it does seem like that is happening, however I really don’t know how to fix that…

1 Like

By the way, what is the variable “CountUpToTime” for?

Oh whoops, I forgot to delete that…

Here is the new script:

local Time = script.Parent.TimeFrame:WaitForChild("Uptime")
local Play = script.Parent.TimeFrame:WaitForChild("Play")
local Reset = script.Parent.TimeFrame:WaitForChild("Reset")
local Pause =  script.Parent.TimeFrame:WaitForChild("Pause")

local TimerActive = false

local function PlayTimer()
	local Start = tick() 

	while true do
		wait()

		if TimerActive == false then

			local Difference = tick() - Start

			local Minutes = math.floor(Difference / 60)
			local Seconds = Difference - Minutes * 60
			local Milliseconds = math.floor((Seconds - math.floor(Seconds)) * 10) 
			Seconds = math.floor(Seconds)

			if string.len(Milliseconds) < 3 then Milliseconds = "0" .. Milliseconds end 
			if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
			if string.len(Minutes) < 2 then Minutes = "0" .. Minutes end
			--if string.len(Hours) < 2 then Hours = "0" .. Hours end

			local Format =  Minutes .. ":" .. Seconds .. ":" .. Milliseconds

			Time.Text = Format
		end
	end
end

local Create = coroutine.create(PlayTimer)

local function PauseTimer()
	TimerActive = true
	coroutine.yield(PlayTimer)
end

local function ResetTimer()
	TimerActive = true
	Time.Text = "00:00:00"
	coroutine.yield(PlayTimer)
end

Reset.MouseButton1Click:Connect(function()
	ResetTimer()
end)

Play.MouseButton1Click:Connect(function()
	TimerActive = false
	if TimerActive == false then 
		coroutine.resume(Create)
	end
end)

Pause.MouseButton1Click:Connect(function()
	PauseTimer()
	TimerActive = false
end)

Well, you wrote this

Yet you also wrote this in your pausing code

It seems like that is probably the issue. You need to change this line to true:

TimerActive = true

Hope this helps :slight_smile:

Hmm, I tried and it still did not change anything, this is the new code:

local Time = script.Parent.TimeFrame:WaitForChild("Uptime")
local Play = script.Parent.TimeFrame:WaitForChild("Play")
local Reset = script.Parent.TimeFrame:WaitForChild("Reset")
local Pause =  script.Parent.TimeFrame:WaitForChild("Pause")

local TimerActive = false

local function PlayTimer()
	local Start = tick() 

	while true do
		wait()

		if TimerActive == false then

			local Difference = tick() - Start

			local Minutes = math.floor(Difference / 60)
			local Seconds = Difference - Minutes * 60
			local Milliseconds = math.floor((Seconds - math.floor(Seconds)) * 10) 
			Seconds = math.floor(Seconds)

			if string.len(Milliseconds) < 3 then Milliseconds = "0" .. Milliseconds end 
			if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
			if string.len(Minutes) < 2 then Minutes = "0" .. Minutes end
			--if string.len(Hours) < 2 then Hours = "0" .. Hours end

			local Format =  Minutes .. ":" .. Seconds .. ":" .. Milliseconds

			Time.Text = Format
		end
	end
end

local Create = coroutine.create(PlayTimer)

local function PauseTimer()
	TimerActive = true
	coroutine.yield(PlayTimer)
end

local function ResetTimer()
	TimerActive = true
	Time.Text = "00:00:00"
	coroutine.yield(PlayTimer)
end

Reset.MouseButton1Click:Connect(function()
	ResetTimer()
end)

Play.MouseButton1Click:Connect(function()
	TimerActive = false
	if TimerActive == false then 
		coroutine.resume(Create)
	end
end)

Pause.MouseButton1Click:Connect(function()
	PauseTimer()
end)

Could it have anything to do that I am using tick()?

Oh I just noticed that under “play” you just resume the same coroutine when it is clicked, so the Start value is probably the same.

So, would I have to make a new coroutine?

Just for testing, I copied the code exactly and put in the same frames and such and added a print for what value Start is every time it is ran. It confirmed that the Start is the exact same every time.
image

Yeah, so would there be a way to like reset the coroutine?

Sorry that I am confused about this, never used coroutines before.

Here is a working script:

local Time = script.Parent.TimeFrame:WaitForChild("Uptime")
local Play = script.Parent.TimeFrame:WaitForChild("Play")
local Reset = script.Parent.TimeFrame:WaitForChild("Reset")
local Pause =  script.Parent.TimeFrame:WaitForChild("Pause")

local CountUpToTime = 0

local TimerActive = false

local First = true

local Start = tick()

local HasReset = false

local Paused = false
local PauseTime = tick()

local function PlayTimer() 

	while true do
		wait()

		if TimerActive == false then

			CountUpToTime = CountUpToTime + 1

			local Difference = tick() - Start

			local Minutes = math.floor(Difference / 60)
			local Seconds = Difference - Minutes * 60
			local Milliseconds = math.floor((Seconds - math.floor(Seconds)) * 10) 
			Seconds = math.floor(Seconds)

			if string.len(Milliseconds) < 3 then Milliseconds = "0" .. Milliseconds end 
			if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
			if string.len(Minutes) < 2 then Minutes = "0" .. Minutes end
			--if string.len(Hours) < 2 then Hours = "0" .. Hours end

			local Format =  Minutes .. ":" .. Seconds .. ":" .. Milliseconds

			Time.Text = Format
		end
	end
end

local Create = coroutine.create(PlayTimer)

local function PauseTimer()
	TimerActive = true
	coroutine.yield(PlayTimer)
end

local function ResetTimer()
	HasReset = true
	TimerActive = true
	Time.Text = "00:00:00"
	coroutine.yield(PlayTimer)
end

Reset.MouseButton1Click:Connect(function()
	if Paused then
		Paused = false
	end
	ResetTimer()
end)

Play.MouseButton1Click:Connect(function()
	if First then
		First = false
		Start = tick()
	end
	if HasReset then
		HasReset = false
		Start = tick()
	end
	if Paused then
		Paused = false
		local NewTime = tick() - PauseTime
		local OfficialTime = Start + NewTime
		Start = OfficialTime
	end
	TimerActive = false
	if TimerActive == false then 
		coroutine.resume(Create)
	end
end)

Pause.MouseButton1Click:Connect(function()
	Paused = true
	PauseTime = tick()
	PauseTimer()
	TimerActive = false
end)

So, basically, what I did was move the Start variable out of the function and would change it each time it reset, and when it was paused update the time so it doesn’t just skip some time. If you need to know the specifics, you can look through the script. Also, sorry that the script is really messy :sweat_smile:
Edit: I need to add the TimeFrame back in I forgot to put it and provided a non-working script (It should work now).
Edit 2: I just finished adding like 3 lines of code to completely fix it lol.

1 Like

Ok, there is a slight issue, there is negative time when you reset the timer and then start a new one:

https://gyazo.com/3db454fb25a93fad5d1cefa0da15c9bb

(Idk if it is just for me, or not).

I just tested it out and this code completely works. I modified it a bit:

local Time = script.Parent.TimeFrame:WaitForChild("Uptime")
local Play = script.Parent.TimeFrame:WaitForChild("Play")
local Reset = script.Parent.TimeFrame:WaitForChild("Reset")
local Pause =  script.Parent.TimeFrame:WaitForChild("Pause")

local TimerActive = false

local Start = tick()

local function PlayTimer() 

	while true do
		wait()

		if TimerActive == false then
			
			local Difference = tick() - Start

			local Minutes = math.floor(Difference / 60)
			local Seconds = Difference - Minutes * 60
			local Milliseconds = math.floor((Seconds - math.floor(Seconds)) * 10) 
			Seconds = math.floor(Seconds)

			if string.len(Milliseconds) < 3 then Milliseconds = "0" .. Milliseconds end 
			if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
			if string.len(Minutes) < 2 then Minutes = "0" .. Minutes end
			--if string.len(Hours) < 2 then Hours = "0" .. Hours end

			local Format =  Minutes .. ":" .. Seconds .. ":" .. Milliseconds

			Time.Text = Format
		end
	end
end

local Create = coroutine.create(PlayTimer)

local function PauseTimer()
	TimerActive = true
	coroutine.yield(PlayTimer)
end

local function ResetTimer()
	TimerActive = true
	Time.Text = "00:00:00"
	coroutine.yield(PlayTimer)
end

Reset.MouseButton1Click:Connect(function()
	ResetTimer()
end)

Play.MouseButton1Click:Connect(function()
	Start = tick()
	TimerActive = false
	if TimerActive == false then 
		coroutine.resume(Create)
	end
end)

Pause.MouseButton1Click:Connect(function()
	PauseTimer()
	TimerActive = false
end)
1 Like

Oh, let me fix that real quick. I have just noticed that too. When you pause the timer and also reset it, it will go into negative for a second. I will edit the script I sent when I am done.

The code I sent completely works just fine.

No, the pausing doesnt work properly.

Yeah, the pausing resets the time.

Oh let me change that then. That will be a minor change. Just a second.

I already fixed it in my code I edited my original post.