Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Have a script wait precisely?

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?

6 comments

0

I don't really understand what you're saying, can you rephrase?

0

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.

0

Oh, I see. So like, when you use a large integer with wait(), all the extra micro seconds add up.

0

Yes..

0

Why do you need this?

0

I need a countdown that exactly counts down a hour. Such as 5:00pm to 6:00pm.

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.