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.