Which one is better? I have a-lot of while loops in my game and its starting to lag the scripts.
Is task.wait() better or should I set the wait() time to a second instead of 0.1 for the loops?
While your performance issues are probably another issue, task.wait() is seen as a upgrade from the global wait(). I believe the global wait() is soon to be deprecated anyway.
If there are a-lot of loops, will the heartbeat slow down?
I think its safe to say the more load on the machine, the larger the time between heartbeats. However it really depends on how often the loop is being run and what code is being completed during it.
One more question, would only using task.wait() for loops and other scripts that minic tweening have less load on the machine?
Most likely minimal effect, the upgrade of task.wait() is that it is more precise and doesn’t occasionally yield for much longer then its supposed to. While it will be beneficial to replace wait()
with task.wait()
if you are having performance issues you will want to rethink your loops code instead.
Well the loops are just target/follow functions to the player. Each one second, it fires.
If you want your loop more precise, you should use task.wait() and if you don’t really care about precision, you may use wait(). Just a note, task.wait() is only a tiny bit more precise than wait().
It seems the difference hasn’t been explained yet.
task.wait()
yields (delays) for a single frame, which at 60 frames per second is around 0.015 seconds, wait()
on the other hand yields (delays) for two frames, which at 60 frames per second is around 0.03 seconds. The difference between using these once is essentially negligible (0.015 seconds), the difference becomes increasingly observable when using them within loops, take the following for example.
task.wait(5)
local timer = tick()
for i = 1, 100 do
wait()
end
print(tick() - timer)
--0.03 * 3 ~ 3
timer = tick()
for i = 1, 100 do
task.wait()
end
print(tick() - timer)
--0.015 * 3 ~ 1.5
A good way to optimize your loops is to find anything that is a calculation and maybe instead of doing it every frame do it every 5 frames. Anything that you Instance.new the properties should be defined before the new object is parented.
I would use task.wait() because on mobile devices wait() can be quite unpredictable due to throttling.