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

Have a script wait precisely?

Asked by
Nickoakz 231 Moderation Voter
10 years ago

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?

0
I don't really understand what you're saying, can you rephrase? YaYaBinks3 110 — 10y
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. Nickoakz 231 — 10y
0
Oh, I see. So like, when you use a large integer with wait(), all the extra micro seconds add up. infalliblelemon 145 — 10y
0
Yes.. Nickoakz 231 — 10y
View all comments (2 more)
0
Why do you need this? YaYaBinks3 110 — 10y
0
I need a countdown that exactly counts down a hour. Such as 5:00pm to 6:00pm. Nickoakz 231 — 10y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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.

Ad

Answer this question