A function that allows you to wait up to 0.000001 second and lower

this function allows you to wait in nanoseconds
its just like a perfect wait

local function NanoWait (seconds)
	
	if seconds <= 0.005 then
		local TimeNow = os.clock()
			while true do
			if os.clock()-TimeNow >= seconds then 
					break 
				end 
			end -- this is for 0.005 seconds and lower.
	else
		task.wait(seconds) -- and this is for higher that 0.005 seconds (don't remove it).
	end
	
end -- By QELPHT

you can use it by writing NanoWait() and but your time in it like this:

NanoWait(0.000001)

this is to check if you don’t believe in it:
input:

local time = os.clock()
NanoWait(0.000001)
print(os.clock() - time)

output:
0.000001400003384333104

this script by QELPHT

2 Likes

I’m not sure what you would need to use this for personally. Using RunService should be sufficient for a fast wait. Good work though.

9 Likes

This would just lag the game. You’re waiting by using an endless non-yielding loop which will hog the maximum allotted resources until it stops.

9 Likes

Sure, there are tricks like this so that your minimum wait time is less than the delta of each frame (task.wait is based on PostSimulation frequency) or ~0.03 seconds (deprecated global wait is based on old 30hz task scheduler), but there are two problems with it.

  1. It’s pretty heavy to run.
  2. If none of the existing wait or yield methods solve your use case, then you’re probably running into a timing problem that you need to fix with defensive architecturing instead.

Don’t rely on novelties like this in production-level experiences for your and your collaborators’ sanity’s sake, pretty please. The perfect wait is not waiting at all; code that’s more responsive towards context is better predictable and therefore maintainable.

9 Likes

This will turn on when your time less than 0.005
That’s means that this is smaller than 1 frame and will not lag

2 Likes

If im running 2000 fos it will lag right?

2 Likes

So it waits for one resumption cycle? Wow, very very useful useless resource.

2 Likes

The funniest part is that no other code will be able to run in the mean time.


If only while loops evaluated an expression before each cycle to determine whether or not the loop should break.

PS: Too bad repeat until totally doesn’t exist at the time of writing… it sure would’ve made far too much sense to use here.


Summary of OP:

User figures out that dedicating 100% of the Luau runtime to polling os.clock results in higher ‘wait’ precision.

7 Likes
local Time=os.clock()

while os.clock() - Time < seconds do end -- even shorter and same thing.
2 Likes

What if I literally don’t use it? Like I don’t think there are any places where you would wait 0.000001 seconds or lower…

2 Likes

This feels like almost a copy of my useless thing, it popping up to my notifications and seeing something similar to my title made me think that I have a clone??

I don’t remember if I’ve mentioned it was useless in the thread.

A better way to do these is to re-adjust your timings whenever the cycle restarts.

4 Likes