Troubles with timer

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