Can someone explain all the events of run service in detail?

It’s all in the title, someone please explain what the three events of runservice are, do and are used for in extreme detail. Of course I already looked it up on the devhub but the explanations I got are like: " Fires every frame after the physics simulation has completed, " Fires every frame prior to the physics simulation". So yeah, it doesn’t answer much.

1 Like

It sounds like you haven’t been clicking around too much because the RunService documentation points to the Task Scheduler for more detailed explanation of these things, including definition of what a frame is. But it might also just be the fact that it’s not always straightforward to understand these things, so I can start pushing you in the right direction:

A frame is a unit of game logic where work is done. Each frame should perform tasks efficiently, leading to a more frames per second and a smoother player experience.

A frame is just that: a unit of game logic. You may also recall frame from the term Frames Per Second (FPS), the rate of which images appear on your screen. The faster your game logic is, the steadier FPS you can achieve.

As for the various events of RunService, they run at different times during a frame. RenderStepped, Stepped and Heartbeat all run every frame, but in different order. Check Task Scheduler/Scheduler Priority to see what runs when. Physics also runs at 240GHz, which means it can occur multiple times per frame. This can be confusing, but just read through the Task Scheduler page and you should get a fairly decent introduction to these concepts.

It is important to use the right event for the right task, as for example anything dealing with camera manipulation should run before the world is rendered in the RenderStepped stage.
If you want to apply changes to something before Physics are simulated, do it in the Stepped stage. Things relying on or reacting to physics should run after physics have been simulated, in the Heartbeat stage.

Hope this helped clear some things up! If you have more questions regarding this, I will answer to the best of my ability. :smile:

1 Like

That was a really good explanation, better than that page on the devhub haha. However I got a couple questions.

Which of these stages does animation fall under? I’m asking because someone advised me to use the stepped event when waiting for the player’s character in this instance of my script:

	repeat RunService.Stepped:Wait() until Player.Character
	local Humanoid = Player.Character.Humanoid
	local Stamina = Instance.new("IntValue",Player)
	Stamina.Value = MaxStamina
	Stamina.Name = "Stamina"
	local Animator = Instance.new("Animator",Humanoid)
	
	Stamina.Changed:Connect(function(Property)
		ReplicatedStorage.RemoteEvents.StaminaUpdate:FireClient(Player,Stamina.Value,MaxStamina)
	end)
end)

and as a result it wound up disrupting the running animations that were playing fine before, I’ve tried switching it to Heartbeat seeing as it’s the last stage of all the physics simulation. The animation works fine when repeat RunService.Stepped:Wait() until Player.Character is removed but then the other parts of the script stops working.

The one who advised you needs to be advised himself.

Don’t use polling for waiting for the player’s character.

You can just do this:

local Character = Player.Character or Player.CharacterAdded:Wait()

Also don’t use the parent argument of Instance.new.
Also don’t use Humanoid:LoadAnimation, that has been deprecated, use the Animator object instead. And keep an eye out on the Announcements category daily.

2 Likes

May I ask what exactly is polling?

Polling is exactly repeat wait() until x. Sometimes it’s called busy waiting, which may be more appropriate, polling and busy waiting aren’t the exact same though. It is basically doing that, repeatedly checking if something is true, which is bad. Opt for event-based programming, which is what @StrategicPlayZ suggested by waiting for the event to fire.

Here is an excerpt of a useful topic.

2 Likes

So the reason the animations were disrupted was because it was infinitely yielding my script?