Hello, 2 years ago I ran across the concept of RunService. I was curious to see what it was as it was used often, I know what it does but I have no idea why it should be used. Especially how the functions work.
I’m aware that RunService runs per frame, but if that’s the case, why are there multiple functions for it?
And how can these multiple functions be used, and what are they commonly used for.
Runservice has a good amount of functions like IsStudio, IsServer, Heartbeat, and RenderStepped. RenderStepped and Heartbeat are typically used for smooth rotations, movement, or just checks. IsServer can be used to check if the game is in an actual server instead of studio, and IsStudio can be used to check if it is being run in Studio. There is also Stepped, which people (most of the time) use to tell how many frames a second the player is getting.
You generally use the RunService for its events, like RenderStepped and HeartBeat. Events are things that happen, and you can write callback functions that run when the event happens. For example, if you want to run some code every frame, you can use RunService.RenderStepped, which is an event that happens each time a new frame is rendered:
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
print("This code runs every frame!")
end)
Notice how we “Connect” the function to the event. This means every time the event happens, our callback function runs to handle it.
The delta time thing is the difference in time since the last RenderStepped. (Events often have helpful parameters that your callback function can use.) For example, you could use delta time to get the frame rate like this:
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function(deltaTime)
print(math.round(1/deltaTime).." frames per second!")
end)
This code tells you the frames per second every frame.
Note what Forummer means by this is only use it when you should. If you need code to run every frame or every physics update, use RunService.RenderStepped and RunService.Heartbeat. Otherwise, don’t poll for values constantly and basically just use RunService connections as while loops. Instead, use an actual event for what you want.
For example, instead of using RunService.Heartbeat and checking the camera’s position, use Camera:GetPropertyChangedSignal(“CFrame”) and then check the camera’s position.
Stepped happens before physics while Heartbeat happens after physics. Therefore, gameplay logic that affects the physics state should be done in Stepped , such as setting the Velocity of parts. In contrast, gameplay logic that relies on or reacts to the physics state should be handled in Heartbeat , such as reading the Position of parts to detect when they enter defined zones.
I see, I’m currently making a combat system. So would using Delta Time allow me to make something like counters? Or add end lag? For example if I would use the “counter” move it would run after 8 frames have passed instead of relying on the normal loops which could arise problems?
I’d recommend “.Stepped” for physics-related loops. It fires every frame prior to the physics simulation. Unless the physics is regarding the player’s character, in which case “.RenderStepped” is more favorable.