An Accurate Real Wait Time

So recently when I was making this tutorial and I thought to myself why not use os or tick to make an accurate waiting time. So I decided to make it with os.clock(). It seemed to work perfectly and I made a small demo of it. The demo isn’t perfect because of human error(I think).

Here’s my module script :

local osRealWait = {}

local OS_CLOCKValue = script["os.clock()"]

return function(Wait_Time : number)
	local NowTime = os.clock()
	local ProjectedTime = NowTime + Wait_Time
	repeat task.wait()
	until OS_CLOCKValue.Value >= ProjectedTime
end

Inside the module I have a value and a script in the value changing the value’s value to the current os.clock() time.

Here’s the demo link : osAccurateWait - Roblox

Is this a good way to do this or on how I can improve it?
Thanks
@Tomroblox54321.

That wouldnt work since you arent changing the value to anything.

1 Like

I actually am changing the numbervalue’s value on every .Stepped frame.
Here’s the script in the number value:

game:GetService("RunService").Stepped:Connect(function)
script.Parent.Value = os.clock()
end)

Isn’t this just useless overall? In theory task.wait() would actually be more efficient. Furthermore, they would both be as accurate.

I’m not totally sure about that. I’ll do some benchmarking to see.

It’s a micro fraction of a second better.

Output
  12:55:13.780  10.009538208018057  -  Server - osAccurateWaitTest:5
  12:55:13.780  10.009840834070928  -  Server - Script:3

Edit :
Got it a bit better

  12:58:09.198  10.004809540929273  -  Server - osAccurateWaitTest:5
  12:58:09.198  10.005382749950513  -  Server - Script:3

I’ll see what I can do to improve it.

How are you benchmarking task.wait() and your “accurate” wait.

Just using a simple os.clock()

Somehow wait() won against mine.

  13:04:25.603  10.016003584023565  -  Server - Script:3
  13:04:25.603  10.016367333009839  -  Server - osAccurateWaitTest:5

I don’t see why this is more practical than task.wait(). What you’re doing is creating a more inefficient version of task.wait() because task.wait() is based on heartbeat. It’s very rare that you would need something to yield at an accuracy higher than that error margin.

1 Like

You cant get more accurate than mil seconds bcz roblox only supports a CPU Tickrate of mil secs

The most CPUs dont have an more accurate tickrate than mil secs, and i dont see a problem with using task.wait

In all honest I have absolutely no idea what you are trying to do here.

Why on earth are you trying to get an accurate wait time?

Side note:
wait() is quicker that task.wait() since the latter is yielded until the next frame whilst the first one isn’t.
wait() is affected by throttling that is why using task.wait() is generally better.

1 Like

Ok thanks for answering about wait.