What is RunService?

What is RunService and what does it do? I looked it up on the devhub and still don’t get what it is and does from the explanation given:
“RunService contains methods and events for time-management as well as for managing the context in which a game or script is running. Methods like IsClient, IsServer, IsStudio, can help you determine under what context code is running. These methods are useful for ModuleScripts that may be required by both client and server scripts. Furthermore, IsStudio can be used to add special behaviors for in-studio testing.”

What in the world does that even mean lmao, what is context code? IsClient? IsServer? IsStudio?

1 Like

Those methods such as IsStudio, IsClient etc, help you determine under what context the code is running, IsStudio will return true if the current environment is run in studio. The same for the other two, IsServer is true when the code is running in the server side.

Use cases are really a lot, a simple one can be a same module, same function but different functionallity.

Example:

local module = {}
local RunService = game:GetService("RunService")

local isServer = RunService:IsServer()

function module.test()
   if isServer then
       print("Server Side!")
   else
       print("Client Side!")
   end
end

Now when this module required from server & client, and the test function is ran, it will print differently on both the contexts.

RunService also has events such as .Heartbeat, .RenderStepped and .Stepped that.
For example, RenderStepped fires every frame, prior to it being rendered. It also passes a step time that indicates the time since last frame.

1 Like

RunService is a service that can be an alternative of while true do loop, making FPS and more.

:IsClient(), :IsServer() and :IsStudio() functions are used for checking where is code running at.

For example if you run a game on studio with a script inside ServerScriptService with a line containing:

print(game:GetService("RunService"):IsClient(), game:GetService("RunService"):IsServer(), game:GetService("RunService"):IsStudio())

it will output to console as “false true true” since script itself runs on server and on studio at the same time so both :IsStudio() and :IsServer() functions returns true.

If you try the same thing but with a localscript inside somewhere where it can run it will output to console as “true false true” since :IsClient() returns true when used in a localscript and vice versa with :IsServer().

Of course that’s not the only use for RunService. There are 3 important events it contains which are Stepped, RenderStepped and Heartbeat.

Stepped event fires fires every frame before physics simulation takes effect.
Heartbeat is same except it fires after every frame which physics simulation is completed for that frame.
Both Stepped and Heartbeat events fires 60 times in a second on server under normal circumstances and on client unless client’s computer can’t handle the game properly which may slow down how fast does these events fire.

RenderStepped event on other hand fires every frame before frame gets rendered and RenderStepped’s fire rate is also dependent to client’s computer when used on client.

Edit (11/02/2021(DD/MM/YYYY)):

Stepped, Heartbeat and RenderStepped events will be deprecated soon once new 4 events are added to RunService. Here is the updated version:

PreRender: Equivalent of old RenderStepped event. Fires at every frame, after user input is handled and before rendering operation of frame starts. Both PreRender and RenderStepped can be only used on localscripts or modulescripts required by localscripts and fire rate of the event is dependent on clients FPS rate. This is the 1st event that fires, out of 4.

PreAnimation: This is the only truly new event out of the 4 new events. It fires before humanoid animations are applied. This fires after PreRender event.

PreSimulation: Equivalent of old Stepped event. It fires before simulation of physics starts. This is the 3rd event that fires after PreAnimation event.

PostSimulation: Equivalent of old Heartbeat event. It fires after simulation of physics finished. This is the event that fires in last place.

NOTE: These events are not added to API yet. Do not try to use it in new work.
Source: https://twitter.com/MaximumADHD/status/1357532484154580995

6 Likes

Very good and concise explanation, however what is context code?

You’re pretty confused with those two words, but they aren’t the same. What it means is, the environment the code is running in.

2 Likes

It’s weirdly worded. Context code isn’t a thing, so I’d assume it’s trying to say “under what context the code is running.”

4 Likes

Ahhh okay, it is indeed weirdly worded haha.