User:JulienDethurens/Essays/Wait function

From Legacy Roblox Wiki
Jump to navigationJump to search

If you do not know yet what the wait function is and how to use it, then learn that first.

The wait function is well known and extremly common. Yet, there is some confusion about it.

The wait function accepts one argument and waits that much time, in seconds. However, what if you give it zero as an argument, or if you give it no argument at all? And what if you give it a negative argument?

Most people will tell you that wait() and wait(0) will wait 0.03 seconds, or roughly 0.03 seconds. This is false.

Some others will tell you that wait() waits a frame. This is false too.

Finally, some others will tell you that wait() and wait(0) do not wait at all. This is even more false than the previous statements.

Here is how it really happens:

When you call the wait function, ROBLOX will yield (pause) the current thread (the current piece of code that is running) and will check every frame if the time specified in the argument or more has passed. If this is the case, it resumes the thread.

This explains lots of things that you might have noticed with the wait function.

You might have noticed that the wait function does not wait exactly the time you send it as an argument. It returns how much time exactly it waited, and you will notice that it is often a little different from the time you wanted it to wait. This is because the first frame after the time you asked it to wait for was not exactly at the time you asked it to wait for, but a little after.

You might also have noticed that it is impossible to wait less than one frame of time. This is because the wait function will always yield the thread at least until the next frame, notice the time has passed already and resume the thread.

If you sent a negative value to the wait function, it will just do the same thing as if it was zero. The wait function does not really wait, as its name says, it just checks every frame if enough time has passed, and, if it is the case, resumes the thread.

But what if you didn't send it any argument? You might think it just uses zero. But it doesn't. It actually uses the DefaultWaitTime setting (settings().Lua.DefaultWaitTime), which you can change from the studio's settings window, from the command bar, or even from the properties window if you manage to select the settings object in the explorer (requires modification of the ReflectionMetadata.xml file and requires to put the settings in a visible object, though you could also do it with the Selection service). However, the fact it uses the DefaultWaitTime setting usually doesn't change much, since that setting is by default set to zero. Be careful, though, when you use the wait function with no argument in a LocalScript, because the user might actually have edited his settings and the wait function might not wait for only a frame like you'd expect it to do. Because of this, and because specifying the argument instead of letting the wait function getting it from the setting is less expensive in performance, it is more efficient and safer to always use zero explicitly as the argument instead of not sending any argument at all. Usually, you won't have to worry, though, as most users don't even know about the existence of that setting. But, heh, who knows, an user might actually find some way to cheat in your game by editing it in a way that advantages him...

External links