Hello, everyone.
The question is simple. Why is wait() important? I see tutorials and people putting wait in every iteration of loops.
Sincerely,
-coolguyweir
Hello, everyone.
The question is simple. Why is wait() important? I see tutorials and people putting wait in every iteration of loops.
Sincerely,
-coolguyweir
If your loop has a large amount of iterations (or maybe infinite) then you will cause lua to hang because too much is happening approximately at once.
Also it is suggested to use task.wait()
over wait()
now.
One of the most common uses is in while loops.
Most people would use this format when making a while loop.
while true do
wait(--[[number here]])
--[[
Code Here
]]--
end
but an easy way to simplify that is by doing this.
while wait(--[[number here]]) do
--[[
Code Here
]]--
end
More experienced programmers suggest while wait(--[[number here]]) do
, because this shortens the development process. but you will see while true do
in a lot of YouTube tutorials, and that format is mostly used to understand the script more for beginners, being one of the main reasons it is used in YouTube tutorials.
but, doing so will make fast loops slower.
What is task.wait()?
gotcha. Thanks for the suggestion!
Making loops go too fast can crash your game if it lasts too long, if you only loop a certain amount of times like a for loop then its fine, but a while loop should have a task.wait().
Itâs essentially the same function as wait()
, but should be used because wait()
is being deprecated soon.
Keep in mind when doing it the second way that it will wait BEFORE running your code versus the first one that waits AFTER your code executes.
but what is task.wait()? What is its actual purpose compared to wait()?
yup yup thatâs true. But shouldnât be that long of a wait most cases
really? Well okay then. But they functional completely normally?
wait() is being deprecated in the terms that Roblox donât want you to use it. But actually removing support for it will break way too many games on the platform, so it will keep existing & functioning. The problem is wait() throttles a lot & isnât very accurate. task.wait() is essentially basing directly on the heartbeat which is ran each frame. So you should safely expect it to be done waiting on the exact frame itâs due, which wait() often doesnât.
Itâs just much more reliable.
For this case no, but for longer waits (i.e., 1 second+)
task.wait() is what you want to use. Itâs a lot more accurate than just using wait()
The main reason for task.wait (and its deprecated friend, wait) is to pause your up for a short period of time.
The time, with no argument defined is 1 resumption cycle (generally 1 frame since Roblox cycles are 60hz). It can, however be modified to wait as long as you want it to.
You should be using it in an infinite loop (such as while true do) to ensure your game doesnât freeze. Roblox needs to know it can pass onto the next frame of operations knowing your code has completed, creating an infinite loop with no pauses basically means Roblox will never be able to proceed since it thinks your code is still running.
I can give you a more technical rundown but youâre a beginner so I dont really want to give you too many technical terms.
There is one down-side about task.wait()
, it continues the loop even though you either disabled or destroy the script.
Although wait()
is not encouraged to be use, wait()
corrects this flaw.
oooo okay got it! thank you for the explanation!
âcreating an infinite loop with no pauses basically means Roblox will never be able to proceed since it thinks your code is still running.â
but, canât we have if statement that checks and breaks accordingly?
âit continues the loop even though you either disabled or destroy the script.â
So basically it will continue the loop even though you canât see changes, which takes up memory?
It should be
Yes. It takes up untracked memory.