I can’t think of a situation where you’d want to use wait()
over task.wait()
No. Definitively not, task.wait
is definitively better.
The differences with wait
and task.wait
are:
-
wait() can sometimes delay when it resumes the thread because of performance concerns, which is actually bad because you will rarely have those because of it
-
task.wait() updates 2x faster than wait(), therefore more accurate
Also:
Just note that task.wait
, just like wait
is still a code smell, usually indicates a bigger structural problem with your code.
wait() also returns a second value which describes the time that it finished yielding (basically os.time()). An example of this can be found in the Roblox default animate script. Note that it’s not worth using wait() over task.wait() specifically for this reason. You can simply call os.time after your yield to replicate the behavior.
task.wait(...)
is based off of RunService.Heartbeat
meaning that task.wait(...)
is == RunService.Heartbeat
. If you add parameters, it will keep yielding RunService.Heartbeat
until the number you replace with ...
's time is up. Idk how wait works, but DONT USE IT. It is old, legacy, and will become deprecated soon.
Thanks for the help you guys! If anybody else wants to add more info, feel free to do so.
Just wanted to chime in and say that while we haven’t deprecated wait
yet we intend to do so in the future. That’s not to say it will stop working, just that you should use task.wait
going forward.
Oh I thought you meant you’d remove wait() one day.
He said in his post:
Why didn’t you just change the function of wait() to be equivalent to RunService.Heartbeat:Wait() instead of choosing to create a new syntax for it?
(you chose to create task.wait() instead of just changing what wait() does. why?)
This is to preserve backwards compatiblity with older games (and even some modern ones).
If they just changed/updated the wait
function, many games on Roblox will start to experience inaccurate yield times.
I thought task.wait() was a more accurate yield? how would it be inaccurate?
It does, I was referring to wait
. task.wait
runs at 60 Hz while wait
runs at 30 Hz. For example, running wait(0.1)
is not the same as task.wait(0.1)
.
This would break older games that rely on a 30 FPS yield time rather than a 60 FPS yield time
If you have command bar open, type in:
print("wait: ".. wait(),"task.wait: " .. task.wait())
output:
wait: 0.030285800000001473
task.wait: 0.000014499999998918156
you make an excellent point lol
if you try close coroutine that waiting by wait(), youll get error “cannot resume dead coroutine” without any information about error source
Just to clarify we do not guarantee that task.wait
will resume at 60 Hz, otherwise your explanation is correct.
(post has been removed by author)
agree with this. there are many situations where yielding is the only sensible answer, and event based programming is sometimes an unnecessary application
I replaced every repeat loop in my game to reduce lag or such with game:GetService(“RunService”).Stepped:Connect(function() --code end) and the loops worked faster but every wait stopped working putting anything with wait(?) to not continue.
.Stepped doesn’t actually yield while your code is running, so you’ll need to add a debounce or use deltaTime to wait. You should also consider using task.wait() if you aren’t already.