In which case should i use task.wait() in a loop?

Hello, I have a lot of loops in my game (many for i,v and some repeat and while).

I know it’s possible to use task.wait() to slow down the loop, and from what i’ve heard it is better for performance, but I don’t know when I should use it? Here are a few examples to illustrate what I mean. Do you know when i should use it and when i shouldn’t use it ? Do you usually put a task.wait() or never ? Thank you for the help

for i,v in workpace:GetChildren() do
print('hi')
task.wait()
end
for i,v in workpace:GetChildren() do
print('hi')
end
local connexion = true
while connexion do 
print('hi')
task.wait() -- Or without
end
1 Like

It almost entirely depends on how intensive the loop is. If the loop will break in a small amount of time, its probably not neccessary.

for i,v in workpace:GetChildren() do
print('hi')
task.wait()--not really necessary unless there is a large amount of parts in the workspace
end

the point of waiting inside of a loop is to let the processer take a break. code will run as fast as possible, so a unregulated loop will take all the resources it can get to run as fast as possible (I think a loop can loop 60 million times a second, but I don’t remember)

a for i,v in pairs loop would loop mabye… 30, 500 times. it depends. but that’s nowhere near as many times as (as fast as possible)

2 Likes

Alright thank you for your help , i’ll do some test to see

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.