Right now I use while task.wait() do for my loop, is there any better solution , more efficient and consume less memory usage? Thank you.
Try " While wait() do " loop, If it is this type of loop.
Heartbeat is the same thing as doing task.wait()
task.wait()
has replaced wait()
, you should always be using task.wait()
.
RunService.Heartbeat:Wait()
and task.wait()
are basically the same thing, so if you wanted to use an event instead of a loop, you could use Heartbeat
. It might be better for you if you want to disconnect the event to stop it from looping, and since its an event instead of a loop 1 error won’t break the entire loop.
you could do something like RandomEventOccured:Wait()
(ex. workspace.Gravity.Changed:Wait() would continue whenever the gravity changes)
depends on what the context of the script is
Alternative that is accurate to 1 microsecond to 100 nanosecond
function waitAccurate(ts)
local t = os.clock()
while os.clock()-t < ts do
end
end
You can use RunService
's HeartBeat
function on the client, this will fire every frame if I remember correctly
Is it better than using while task.wait() loop?
So I supposed using while task wait loop has the same ‘quality’ as RunService Heartbeat?
It depends on your use case. RunService fires every frame so youd want to use it for more important things like if you have FPS arms, make them attached to the camera. or if you have something like a round countdown timer youd want to use a while wait() do loop
Im not good at explaining things probably someone else could explain it better lol
No, RunService.Heartbeat
and task.wait()
are practically the same thing. a while task wait loop and heartbeat event are basically the same, except Heartbeats can be disconnected a bit easier, and won’t stop running if an error occurs.
I’d use a Heartbeat over a while loop, but its more preference if anything.
That would be RunService.RenderStepped
, not RunService.Heartbeat
.
Ah shoot my bad for the misinformation, I never really payed attention to the docs so I may have overlooked this. Ill keep this in mind next time I inform people about such a topic, thank you
Don’t do that. That will lag your game.
Instead, do
while game:GetService("RunService").Heartbeat:Wait() do
-- stuff here
end
You can use:
while task.wait(0.05747126436781609) do
-- Add anything on here, i don't have ideas.
end
-- While true do is the same as while task.wait() do
So yeah, i hope this helps…
His and your script will do exactly the same thing. If anything his version is better because its an event and not an infinite loop.
How will it lag your game if they are practically the same.
Honestly, what you use depends on what you are trying to do.
If you are trying to wait until something happens, events are usually better than while loops like these. If you are updating for example your camera or a part’s CFrame, do it using RunService depending on your use case.
while true do
--Code here.
task.wait()
end
Would be better since the iteration structure’s condition wouldn’t need to be evaluated to check for its ‘truthiness’.
RunService is your best friend, less memory usage, same (if not faster) speed
In my past experience, I had a system run off of heartbeat, and it was lagging the game, Immediately when I switched it to a loop that has a yield of every Heartbeat, that fixed the lagging issue…
Because Heartbeat spawns a new thread for each function call. If you run Heartbeat and wait in it, the next heartbeat will execute at the normal time.
But if you do that in a while loop, the loop will wait extra for that yield.
This has happened with me before as well and this is the most likely reason for it which I can think of.