Need Help understanding RunService

I’m new to scripting and I came across people using game:GetService(“RunService”). I only know that when I test it is similar to while wait() do. Can someone tell me what the differences are?

2 Likes

I think you are talking about RunService.RenderStepped. It fires everytime a frame is rendered.

For example:

local runService = game:GetService(“RunService”)

runService.RenderStepped:Connect(function()
   print('New Frame!')
end)

I recommend you to read this: RunService | Documentation - Roblox Creator Hub

1 Like

So basically the event fires when there is a new frame?

Yes, but it works only on client.

1 Like

A while-loop will repeat as long as a specified condition is true. Using true as the condition will make it repeat indefinitely, and people add a wait(some number) statement to control the frequency of the loop. However this is not a good idea since the wait function may not be reliable in some cases.

The RunService provides 3 events: Heartbeat, Stepped, and RenderStepped. These events are fired either every render frame (specifically if you use RenderStepped in client code) or every time a step occurs in the physics engine (Heartbeat and Stepped). They run at a variable frequency but tend to be around 60hz. They all include a parameter that tells you the amount of time that passed since the last frame, which you can use to accurately synchronize things like timers, things moving at specific speeds, etc. so that your code would not need to depend on the engine running at a specific frequency.

What also makes them preferable is that unlike loops, they won’t hang your script, and event-driven systems are better practice in general.

Here are some popular explanations that I found:

3 Likes

For the same as runService.Heartbeat?

Take a look:

There is a better explanation than mine lol

1 Like

So I should replaced while true do with RunService.RenderStep?

It depends on what you are trying to accomplish.

1 Like

Okay thanks I understand it now.