Reputation: 25
Timer script stops at 2:29 and won`t count down from there. It's supposed to count down to zero, but will stop after 1 loop. The while true do loop keeps going, but either the text label won't display it, or the minutes and seconds variables aren't changing. I need help making this work.
local starterGui = game:GetService("StarterGui")
local Guis = starterGui.RoundTimer --Includes the time textlabel.
local Seconds = 30
local Minutes = 2
repeat
wait(1)
if Seconds < 9 then
if Seconds == 0 then
Seconds = 59
Minutes = Minutes - 1
else
Seconds = Seconds - 1
end
Guis.Time.Text = tostring(Minutes)..":0"..tostring(Seconds)
else
Seconds = Seconds - 1
Guis.Time.Text = tostring(Minutes)..":"..tostring(Seconds)
end
until Seconds < 1 and Minutes < 1
Upvotes: 1
Views: 499
Reputation: 93
I've known this for a bit now, but in case anyone wants to know what worked:
while true do
wait(1)
local timeleft = game.ReplicatedStorage.Seconds.Value -- the seconds value
local minutes = math.floor(timeleft/60) -- getting the amount of minutes by dividing seconds by 60
local seconds = timeleft%60 -- gets the remaining seconds by using modulo 60 with time left.
script.Parent.Text = string.format("%d:%02d", minutes%60, seconds) -- formats this into a timer.
end
More information about string.format()
can be found here: https://developer.roblox.com/en-us/articles/Format-String
How %/modulo works:
Say you have 90 seconds and want to turn it into 1:30. You've already figured out that by using timeleft/60 you can figure out that there is one minute on the clock. Using timeleft%60
divides time left by 60 and then returns the remainder, therefore giving you 30.
Upvotes: 0
Reputation: 26744
I don't see any problem with the overall logic, so no reason for it to stop at 2:29, but there are some problems with formatting, as this is what I get when I run the script (a fragment):
1:10
1:9
1:8
1:07
1:06
1:05
1:04
1:03
1:02
1:01
1:00
0:059
0:58
As you can see, :8, :9, and :059 are formatted incorrectly.
Something like this may work a bit better:
repeat
Guis.Time.Text = ("%d:%02d"):format(Minutes, Seconds)
wait(1)
Seconds = Seconds - 1
if Seconds < 0 then
Minutes = Minutes - 1
Seconds = 59
end
until Seconds < 1 and Minutes < 1
Upvotes: 3