1

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
1
  • print the condition right before the end of the loop, something like print(Seconds, Minutes, Seconds < 1 and Minutes < 1) and see what you get. Commented Jul 20, 2021 at 19:43

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

Using this code still sadly does not fix the problem of the timer stopping early. Thanks for your help though!
Same issue as @rml1310. Probably fixes the formatting issues but doesn't fix it stopping early.
Yes, there is no problem with code - may be place of execution is time limited?
As I said, I don't see a problem with the overall logic, so the issue is likely to be outside of the code that is shown. If you want to troubleshoot it further, I'd add print statements around the wait(1) call (just to be sure that the control is not blocked inside of it) and after the loop (to confirm that it's not exited prematurely).
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.