I made a custom wait function and would like your thoughts on it. It is very easy to make, use, and explain, so feel free to try it for yourself!
The function code:
local function wait(TIME: number?)
local t = tick
local i = t()
TIME = TIME or 0.01
local w = task.wait
local r = math.random
while t() - i < TIME do
if r(1,10000) == 1 then w() end
end
return t() - i
end
The explanation:
1: The first line declares the name of the function and states that the function may have one integer argument, but the â?â also lets it know that we can leave it blank.
- Note: make sure you have type checking on default or strict mode.
2-3: The next two lines create variables to tick and set up a starting time so we can measure how long has passed.
4: We now set TIME to itself if it exists, and set it to a default value of 0.01 if it is nil (missing).
5-6: We set reference variables to math.random and task.wait to use later.
7: We start a while loop that will keep going until the time passed (calculated by
current time - start time) is greater than or equal to the time amount set by the function caller.
8: A debounce value that will ensure the while loop stops every so often (1 per 100000). Without this, large wait times will cause a script execution overflow.
9-11: We end the while loop and afterward return true from the function. This allows us to do while wait() do because it is true. Finally, we close our function with the end.
How to use:
Call it by simply doing wait() and either provide a wait time or not. The default wait time is 0.01 so, if you want it faster, make it even smaller and specify it as the argument. You can use It alone to halt program execution, or in a while wait() do.
Accuracy and Speed:
Accuracy: This is the only accurate wait function below normal wait() and task.wait() time bounds. Comparing to wait and task.wait:
For 0.001 second counting:
wait() â normal: ~ 0.57
task.wait(): ~ 0.026
wait() - new: ~ 0.001
For 0.1 second counting:
wait() â normal: ~ 0.2
task.wait(): ~ 0.14
wait() - new: ~0.15
For 1 second counting:
wait() â normal: ~ 1.06
task.wait(): ~ 1
wait() - new: ~ 1
For 100 second counting:
wait() â normal: ~ 100
task.wait(): ~ 100
wait() - new: ~ 100
Conclusion of accuracy: It does not matter which you use for normal timeframes, so just go ahead and use task.wait for that. Where the new function really shines is below the othersâ bottom clamp. The others are totally inaccurate with really small times, whereas the new wait is extremely accurate.
Conclusion and Final Notes: If you need a really fast loop, or a really accurate small timekeeper, this function will be useful! Please let me know if I should repost this in resources, and share any and all bugs that you may find!