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?
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.