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).
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.
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)
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()?
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.
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
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.
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)
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.