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

Why is this timer script not working right?

Asked by 9 years ago

So the timer script is supposed to stop once everything reaches 0. However, it doesn't, and I don't know why. The script is pretty crude, so that could be a problem. Can someone please help me?

Here's the script (Yes, everything is defined):

local Seconds = script.Parent.Seconds
local Minutes = script.Parent.Minutes

while true do
    if script.Parent.TimesUp.Value == false then
    Seconds.Value = Seconds.Value - 1
        if Seconds.Value == 0 and script.Parent.TimesUp.Value == false then
            Minutes.Value = Minutes.Value - 1
            Seconds.Value = 59
        end
        if Seconds.Value == 10 and Minutes.Value == 0 then
            script.Parent.Backup.Value = 0
        end
    end
    if script.Parent.Backup.Value == 0 and Seconds.Value == 0 then
        script.Parent.TimesUp.Value = true
    end
    wait(1)
end

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The problem is that you are skipping 0 seconds. -- That actually means your "minutes" are 59 seconds long.

Look at line 7 and then 9; now look at line 15.

Like 7-9 say: "If seconds are 0, then seconds will now be 59."

Immediately following, line 15 asks, "are seconds 60?" which will clearly always be no.


I think the solution to fix both of these problems is to change 59 to 60, and check for 60 or 0 (0 only for when the timer starts at 0 seconds).


If you want a better overall way of doing it, here it is:

Only count in seconds. (If you need to generate a time from a minute, second pair just use 60 * minutes + seconds)

We can still get the number of minutes out by using remainder after addition.

Here is what it would look like:

while seconds > 0 do
    wait(1)
    seconds = seconds - 1
    print( math.floor(seconds / 60) , ":", seconds % 60)
    -- minutes : seconds
end
Ad
Log in to vote
0
Answered by 9 years ago

This should be more efficient (:

local Seconds = script.Parent.Seconds
local Minutes = script.Parent.Minutes

while true do
    if script.Parent.TimesUp.Value == false then
        if Seconds.Value > 0 then
            Seconds.Value = Seconds.Value -1
        elseif Seconds.Value <= 0 and Minutes.Value >= 1 then
            Minutes.Value = Minutes.Value - 1
            Seconds.Value = 59
        elseif Seconds.Value <=0 and Minutes.Value <= 0 then
            script.Parent.TimesUp.Value = true
        end
    end
    wait(1)
end
0
For some reason that didn't work either CoolJohnnyboy 121 — 9y
0
I've tested it and it worked successfully for me. Do you have the error message? ultimate055 150 — 9y

Answer this question