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

wait() delay with a stopwatch?

Asked by 4 years ago

I'm trying to make a stopwatch, but the timer is out of sync with any other stopwatch. Using some printing, I have discerned that there is a single millisecond delay about every other tick. It is off sync by one second about every minute and a half. As you can tell, 1000 milliseconds do not happen with a half a millisecond delay over 90 seconds. So where did the extra 955 milliseconds come from?

Code:

while true do
    wait(1)
    alive += 1

    textLabel.Text = alive.." Seconds"
end

There was also a much larger delay when ticking 10 times a second, up to a second of delay per two seconds. Also then, every other go took 30-40 milliseconds. The other ones took one millisecond, so once again, where did the extra milliseconds come from?

1 answer

Log in to vote
2
Answered by
imKirda 4491 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

i think it's because wait is never accurate, that's why it returns 2 values and the first one is the actual time it yielded (delta time) so if you waited for 1 second it will return sometimes for example 1.03...

With this you can add the actual time it yielded to the timer by doing this:

while true do
    alive += wait(1) -- adds first value wait returns to it

    textLabel.Text = math.round(alive) .. " Seconds"
end

I used math.round which rounds numbers because you don't want the timer to be 5.234029038528429348237584875283402384027582374 seconds.

Ad

Answer this question