Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Help making my countdown timer GUI more efficeint?

Asked by 6 years ago

Theres gotta be a way to make it less complicated.

Also I cant figure out how to make the timer reset after the time runs out.

local seconds = 0
local minutes = 5

script.Parent.Text = tostring(seconds)
script.Parent.Minutes.Text = tostring(minutes)

while seconds <= 60 do
    script.Parent.Text = tostring(seconds)
    seconds = seconds - 1
    wait(1)
    if seconds == -1 then
        print('1 MINUTE PASSED')
        if minutes > 0 then
            minutes = minutes - 1
            seconds = seconds + 60
        end
        script.Parent.Minutes.Text = tostring(minutes)
    end
    if seconds == -1 and minutes == 0 then
        seconds = seconds + 63 -- Abitrary number to stop the loop
        script.Parent.Visible = false
        script.Parent.Minutes.Visible = false
        script.Parent.Parent.TIMESUP.Visible = true
    end
end

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago
minutes = 2 -- Sets initial timer
seconds = 0

while minutes > 0 or seconds > 0 do -- If timer has time, loop
    seconds = seconds - 1
    if seconds < 0 then -- once seconds have run out, reset seconds and lower minutes by 1
        seconds = 59
        minutes = minutes - 1
        print("1 minute passed")
    end
    wait(1)
    print(minutes, seconds) -- sorry, I couldn't make it fancier
end

print("Achievement get: 2 minutes and 0 seconds wasted") -- Run code when timer runs out

This creates a pretty easy minutes, seconds timer. Optionally, you could only input seconds and divide to get the minutes, seconds.

seconds = 120 -- Sets initial timer

while seconds > 0 do -- If timer has time, loop
    seconds = seconds - 1
    print(math.floor(seconds / 60), math.fmod(seconds, 60)) -- math.floor() returns the minutes. It divides the current amount of seconds by 60 and cuts off the decimal. math.fmod() returns the seconds. It returns the remainder of seconds divided by 60.
    if math.fmod(seconds, 60) == 0 then -- if seconds divided by 60 = 0, print the message. In other words, there are 0 seconds, a new minute started.
        print("60 seconds passed")
    end
    wait(1)
end

print("Achievement get: 120 seconds wasted")

To make the timer reset, you could put the entire thing in a while true do end loop. Whenever the timer is done, it will reset automatically

Also, as TheeDeathCaster mentioned, you could easily just use a for loop for casual purposes.

for seconds = 120, 0, -1 do
    print(seconds)
    wait(1)
end -- Wow! So compact!
Ad

Answer this question