I have a countdown that counts down a hour per second. Over time, the countdown would be over 5 minutes or less then 5 minutes. Is there a way to have wait() perfectly exact?
✓ ANSWER ACCEPTED
Have a script wait precisely?
1 answer
✓ Accepted answer
The solution is to check actual elapsed time rather than assume the previous times add up to something.
Consider the following functions which count to ten in ten seconds:
function pause(time)
wait(time - 0.1 + math.random() * 0.2);
end
-- We use this just to introduce extra
-- variance, for demonstration, although
-- the actual variance in wait is
-- really small.
function loopOne()
for i = 1, 10 do
print(i,10,pause(1));
end
end
function loopSmart()
local last = -1;
local start = tick();
while true do
pause(0.1);
local elapsed = math.floor(tick() - start);
if elapsed ~= last then
print(elapsed+1,10);
end
last = elapsed;
if elapsed > 10 then
break;
end
end
end
loopOne();
-- May take variable amounts of time
loopSmart();
--The prints might be off slightly,
-- (more than with other method),
-- but the overall time is
-- guaranteed to be bounded to within
-- however small you can wait for.
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
I don't really understand what you're saying, can you rephrase?
When you use roblox wait(), it will have roblox decide how much micro seconds to add and puts it with the wait. Like if I request to wait(1), it would sometimes wait(1.01512512241) or wait(0.9995123152). Its what roblox does to allow scripts to have time to run. But I want this script to be precise.
Oh, I see. So like, when you use a large integer with wait(), all the extra micro seconds add up.
Yes..
Why do you need this?
I need a countdown that exactly counts down a hour. Such as 5:00pm to 6:00pm.