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 10 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):

01local Seconds = script.Parent.Seconds
02local Minutes = script.Parent.Minutes
03 
04while true do
05    if script.Parent.TimesUp.Value == false then
06    Seconds.Value = Seconds.Value - 1
07        if Seconds.Value == 0 and script.Parent.TimesUp.Value == false then
08            Minutes.Value = Minutes.Value - 1
09            Seconds.Value = 59
10        end
11        if Seconds.Value == 10 and Minutes.Value == 0 then
12            script.Parent.Backup.Value = 0
13        end
14    end
15    if script.Parent.Backup.Value == 0 and Seconds.Value == 0 then
16        script.Parent.TimesUp.Value = true
17    end
18    wait(1)
19end

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 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:

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

This should be more efficient (:

01local Seconds = script.Parent.Seconds
02local Minutes = script.Parent.Minutes
03 
04while true do
05    if script.Parent.TimesUp.Value == false then
06        if Seconds.Value > 0 then
07            Seconds.Value = Seconds.Value -1
08        elseif Seconds.Value <= 0 and Minutes.Value >= 1 then
09            Minutes.Value = Minutes.Value - 1
10            Seconds.Value = 59
11        elseif Seconds.Value <=0 and Minutes.Value <= 0 then
12            script.Parent.TimesUp.Value = true
13        end
14    end
15    wait(1)
16end
0
For some reason that didn't work either CoolJohnnyboy 121 — 10y
0
I've tested it and it worked successfully for me. Do you have the error message? ultimate055 150 — 10y

Answer this question