A better understanding of RunService

Im learning how to script, and one of the things that has me stuck is run-service as a whole. I dont really understand any of the main functions like RenderStepped, Heartbeat, and Stepped. I tried reading up on it on DevHub RunService | Roblox Creator Documentation -The thread i read

If theres any way i’d like to know what these functions do (please try to simplify it :sweat_smile:) and i’d also like to know how they could be applied in games

Thank you for your time :grinning:

4 Likes
  • RunService.RenderStepped - Runs evertime a new frame is rendered on the client’s device, for example if someone has 60 fps (frames per second), this event runs everytime a new frame is rendered, thus 60 times per second. This runs everytime right before the frame is rendered.

  • RunService.Stepped - In each render like is used in renderstepped, things happen, objects fall down if they were in the sky, etc., physics are applied on them, RunService.Stepped runs right before the physics take effect every frame.

  • RunService.HeartBeat - Same as .Stepped, but runs everytime right after the frame’s physics are finished.

13 Likes

Adding onto this, here’s a pretty helpful image of the actual pipeline.

image

20 Likes

So, if i typed

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
     print("Running _ per second")
end)

this function will run 60 times?

If the client’s roblox is running on 60 frames per second, it will, for a clearer visualisation of what happens when in the rendering/physics loop that happens x times per second depending on the refresh-rate of the client, see the image above posted by Benified4Life

2 Likes

Are there any other functions to run service that i need to know? I read up on functions like IsStudio:() , IsServer(), IsClient() etc

I’d also like to know how RunService could be used in a game? I think knowing how i can use it would make it better for me to learn

fun fact: you can create more faster hearbeat like 120,30,180,240 fps etc
fps: frame per second

1 Like

is only true when you are in roblox studio not player

only true with a script or modulescript required by a script

is true when IsServer() is false

2 Likes

Im having trouble understanding this. So Stepped basically runs a function before the games physics are applied to something?

1 Like

tasks in order

user input
BindToRenderStep
RenderStepped
screen drawn
Stepped
physics calculated
Heartbeat

1 Like

Most functions aren’t very useful practically (some more are useful for plugins), but here’s a few:

  • BindToRenderStep - This function does the same as the following:
local rs = game:GetService("RunService")

rs.RenderStepped:Connect(function()

end)
  • here you can pass the function from the :Connect() as parameter for the BindToRenderStep function
    
  • UnbindFromRenderStep() - essentially undoes what you do with BindToRenderStep(), with the function as argument

  • Pause() - pauses all physics, movement, etc. of the game until Run() is called (inside studio, you have that big button in testing tab for this)

  • Run() - ‘revives’ the game from a Pause() (inside studio you also have a big button in testing tab for this)

  • Stop() - stops the game, shuts down the server, kicks all players, etc. (and yes, even for this there’s a button inside of studio in the testing tab!)

(Sorry for the slow reply after the conversation, wifi died :sob:)

1 Like

I think it is REALLY important to mention that Heartbeat should be used when stressing the CPU (like in big infinite loops), not Stepped.

4 Likes

https://developer.roblox.com/en-us/api-reference/class/RunService