UltraWait: Precise Timing for Roblox

Hello, Developers!

I’m excited to share my first Community Resources post with yall. :tada: Today, I present UltraWait, a lightweight, high-performance module designed to address the limitations of traditional wait methods in Roblox.

:rocket: Why UltraWait?

When it comes to precise timing in Roblox, both wait and task.wait can introduce noticeable inaccuracies, especially for sub-frame durations. For example:

  • task.wait(0.001) may result in an actual wait time of ~0.016 seconds, depending on the device and frame rate.

This inaccuracy can be a dealbreaker for tasks that demand precise timing, such as animations, microsecond adjustments, or ultra-smooth visual effects.

That’s where UltraWait steps in. By combining os.clock() and RunService.Heartbeat, UltraWait achieves sub-frame precision with minimal CPU overhead.

:gear: Features

  • Microsecond Precision
  • Optimized Performance
  • Lightweight and Robust
  • Flexible Usage

:hammer_and_wrench: How to Use UltraWait

  1. Add the UltraWait module to ReplicatedStorage (or any shared service).
  2. Require the module in your script:
-- example:
local UltraWait = require(path.to.UltraWait)

local targetTime = 0.001
local realElapsed = UltraWait.wait(targetTime)
print(string.format("Target: %.3f, Actual: %.15f", targetTime, realElapsed))
Benchmarking
local UltraWait = require(game.ReplicatedStorage.UltraWait)

local targetTime = 0.001

-- UltraWait
local startUltraWait = os.clock()
UltraWait.wait(targetTime)
local elapsedUltraWait = os.clock() - startUltraWait

-- task.wait
local startTaskWait = os.clock()
task.wait(targetTime)
local elapsedTaskWait = os.clock() - startTaskWait

print(string.format("Expected: %.3f", targetTime))
print(string.format("UltraWait: %.15f", elapsedUltraWait))
print(string.format("task.wait: %.15f", elapsedTaskWait))

if elapsedUltraWait < elapsedTaskWait then
	print("UltraWait wins")
else
	print("task.wait wins")
end

Sample Output:

Screenshot 2024-12-06 103921

:open_file_folder: Get UltraWait:

UltraWait.rbxm (1.2 KB)
UltraWait - Creator Store

As this is my first Community Resources post, Id love to hear your thoughts! :bulb: Feel free to:

  • Share feedback about the module.
  • Suggest improvements or use cases.
  • Let me know how it performs in your projects.
3 Likes

Interesting system… But this only has a precision for 0.001 seconds, any higher and it won’t do much.

I also ran a few more benchmarks!
image
image
image
:grimacing:

Seems like your module doesn’t like maths.

It is true that the module is more precise for exact decimals like 0.1 or 0.01 or 0.001 but for fractions… it breaks.

I ended up running a lot more tests and it seems to be a bit random? Sometimes in the same tests roblox wins while sometimes ultra wait. Guess it’s just roblox’s tomfoolery…

Anyways great module!

Over the years there has been many of these made. Be the first and describe a practical usecase for something like this. If you need to do more operations of something per frame at least have a loop with no yield, at most yield the thread at some point in a frame and resume it at a later point in the same frame. You cannot accurately yield with consistent timings within frames.

Exactly - I can’t think of a single use case where precision based waits would be helpful. If there’s a need for sequential events - event based approaches work far more reliably and across different devices with different latencies

1 Like

Even if you somehow find a reason to run multiple code cycles between frames, it only means you designed faulty code to begin with.
Looping without yielding, despite doing this for a very short time, works by using every single available resource available just to run the time, making “Lightweight” definitely a misnomer.