Task.wait(n) vs wait(n)

I’ve heard a lot of people say to use task.wait(n) instead of wait(n). Is there any situations where you would use wait(n) over task.wait(n) or is task.wait(n) just definitively better? Any help is appreciated.

20 Likes

I can’t think of a situation where you’d want to use wait() over task.wait()

7 Likes

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.

48 Likes

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.

5 Likes

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.

4 Likes

Thanks for the help you guys! If anybody else wants to add more info, feel free to do so.

1 Like

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.

51 Likes

Oh I thought you meant you’d remove wait() one day.

6 Likes

He said in his post:

6 Likes

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.

2 Likes

I thought task.wait() was a more accurate yield? how would it be inaccurate?

2 Likes

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

1 Like

If you have command bar open, type in:

print("wait: ".. wait(),"task.wait: " .. task.wait())

output:
wait: 0.030285800000001473
task.wait: 0.000014499999998918156

9 Likes

you make an excellent point lol

1 Like

if you try close coroutine that waiting by wait(), youll get error “cannot resume dead coroutine” without any information about error source

1 Like

Just to clarify we do not guarantee that task.wait will resume at 60 Hz, otherwise your explanation is correct.

2 Likes

(post has been removed by author)

1 Like

agree with this. there are many situations where yielding is the only sensible answer, and event based programming is sometimes an unnecessary application

1 Like

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.