What are the differences between a while wait do or a renderstepped, heartbeat, or stepped?
and what should I use these for?
What are the differences between a while wait do or a renderstepped, heartbeat, or stepped?
and what should I use these for?
Check out this page, it shows the process a frame takes.
Also, never use wait()
because it is unstable and can cause issues. In almost all cases you can replace it with RunService.Stepped:Wait()
.
Why can wait cause issue and become unstable?
This should give you a pretty good explanation.
I would use RunService loops if iâm using a wait()
because it can be unstable sometimes.
Example of a RunService loop:
RunService.Heartbeat:Connect(function()
-- do stuff
end)
RunService doesnât create âloops.â The things you listed are events that are fired at certain points during runtime giving the illusion of a loop. Each event fires at specific points:
For example, you should use RunService.RenderStepped
or RunService:BindToRenderStepped()
for a custom camera system because the camera needs to always be rendered first.
A while loop
is a simple code loop that runs over and over again as long as the condition is true
.
So youâd use this in the case of a round system in a game or a countdown.
All in all, you should use the RunService events for interaction with physical objects in the game and use loops for other things. But thatâs just how I personally use them.
The developer hub has good documentation about these:
wait()
isnât that bad impact if youâre doing something small, but itâs an issue when you used it in core game components.
wait(n)
doesnât actually pause the thread for n seconds, it just make it so that it the number is as close as n, also on top of that, itâs literally pausing the entire thread and youâre just waiting until itâs over. I recommend using os.clock()
to constrict your own wait()
.
while loops go in a individual line but RunServices works like while loop except it runs along with other functions at the same time. If youâre going to make a game that runs a lot of loops at the same time then I would recommend you using RunServices.