Service · inherits Instance
RunService
Service responsible for all runtime activity and progression of time.
About RunService
RunService contains methods and events for time management as well as for
managing the context in which an experience or script is running. Methods like
IsClient(),
IsServer(), and
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.
RunService also houses events that allow your code to adhere to the engine's
frame‑by‑frame loop, such as PreRender,
PreAnimation,
PreSimulation,
PostSimulation, and
Heartbeat. Selecting the proper event to use for
any case is important, so you should read
Task Scheduler
to make an informed decision.
Context Test Results
Properties 1
RunState
: RunStateNotReplicatedThe current run state of the game's simulation.An
RunStatevalue that reflects whether the game's simulation is stopped, running, or paused. Reading reports the current state; setting it transitions the simulation, equivalent toRunService.Run,RunService.Pause, andRunService.Stopfor the corresponding states. Defaults toRunState.Stopped. Setting this property requiresPluginsecurity.The state reported here backs the context methods on
RunService: it isRunningwhenIsRunning()returnstrue, andStoppedwhenIsEdit()returnstrue.
Methods 16
BindToAnimation
(function: Function, frequency: StepFrequency = Hz30, priority: int = 2000) → RBXScriptConnectionBinds a custom function to be called at a fixed frequency before Animators update when fixed simulation is enabled, or immediately beforeBindToSimulationcallbacks otherwise.BindToAnimation()binds a custom function to be called at a fixed frequency which is independent of the frame rate. WhenWorkspace.UseFixedSimulationis enabled, these functions are invoked before Animators update, making this the fixed-frequency counterpart toPreAnimation. When fixed simulation is disabled, they instead run immediately beforeBindToSimulation()callbacks, so no pre-animation ordering should be assumed. In the server authority model, bound functions will also get called when the client needs to resimulate.Like
BindToSimulation(), the bound function is meant for physics controllers and prediction, so an error will trigger if you read or write from unsynchronized properties or call an unsynchronized method. This error is meant to help guide you in writing a pure, synchronized simulation.To synchronize inaccessible properties, you should store the relevant data in attributes and update the relevant property in
PostSimulationorRenderSteppedby reading from attributes. This will leverage attribute synchronization to keep the property fully synchronized through the rollback mechanism.BindToRenderStep
(name: string, priority: int, function: Function) → ()Given a string name of a function and a priority, this method binds the function toRunService.PreRender.The
BindToRenderStep()function binds a custom function to be called at a specific time during the render step. There are three main arguments:name,priority, and whatfunctionto call.As it is linked to the client's rendering process,
BindToRenderStep()can only be called on the client.Name
The
nameparameter is a label for the binding and can be used withRunService.UnbindFromRenderStepif the binding is no longer needed.local RunService = game:GetService("RunService") local function functionToBind() end -- Bind the function above to the binding named "tempBinding" RunService:BindToRenderStep("tempBinding", 1, functionToBind) -- Unbind the function bound to "tempBinding" RunService:UnbindFromRenderStep("tempBinding")Priority
The
priorityof the binding is an integer; it determines when during the render step to call the custom function. The lower this number, the sooner the custom function will be called. If two bindings have the same priority, the engine will randomly pick one to run first. The default control scripts run with these specific priorities:- Player Input:
100 - Camera Controls:
200For convenience; theRenderPriorityenum can be used to determine the integer value to set a binding. For example, to make a binding right before the default camera update, simply subtract1from the camera priority level.
When using
RenderPriority, remember to use.Valueat the end of the desired enum.RunService.BindToRenderStepwill not work if just the enum is used on its own.local RunService = game:GetService("RunService") local function beforeCamera(delta) -- Code in here will run before the default camera script end RunService:BindToRenderStep("Before camera", Enum.RenderPriority.Camera.Value - 1, beforeCamera)Custom Function and Delta Time
The last argument (
function) is the custom function to call. This function will be passed one parameter calleddeltaTimewhich shows how much time passed between the beginning of the previous render step and the beginning of the current render step.All rendering updates will wait until the code in the render step finishes. Make sure that any code called by
BindToRenderStep()runs quickly and efficiently; if code takes too long, the experience visuals will be choppy.- Player Input:
BindToSimulation
(function: Function, frequency: StepFrequency = Hz30, priority: int = 2000) → RBXScriptConnectionBinds a custom function to be called at a fixed frequency which is independent of the frame rate.BindToSimulation()binds a custom function to be called at a fixed frequency which is independent of the frame rate. In the server authority model, bound functions will also get called when the client needs to resimulate. This method is only available whenWorkspace.UseFixedSimulationis enabled.Note that since the bound function is meant for physics controllers and prediction, an error will trigger if you read or write from unsynchronized properties or call an unsynchronized method. This error is meant to help guide you in writing a pure, synchronized simulation.
To synchronize inaccessible properties, you should store the relevant data in attributes and update the relevant property in
PostSimulationorRenderSteppedby reading from attributes. This will leverage attribute synchronization to keep the property fully synchronized through the rollback mechanism.GetPredictionStatus
(context: Instance) → PredictionStatusChecks thePredictionStatusof a specific context instance, useful for debugging scripts affecting multiple instances where some might be predicted and others might not.Clients can use this method to check the
PredictionStatusof a specific context instance, useful for debugging scripts affecting multiple instances (vehicle controllers, custom physics, etc.) where some might be predicted and others might not. This method is also useful for debugging and observing the effects ofPredictionMode.Automaticprediction mode.IsClient
() → booleanReturns whether the current environment is running on the client.If the code that invoked this method is running in a client context (in a
LocalScript, in aModuleScriptrequired by aLocalScript, or in aScriptwithRunContextset toRunContext.Client), this method will returntrue. In all other cases, this method will returnfalse.If this method returns
true, the current environment can access client‑only features likeRunService.PreRenderorPlayers.LocalPlayer.IsEdit
() → booleanReturns whether the current environment is inEditmode.This method returns whether the current environment is in "edit" mode, for example in Studio when the experience is not running.
IsEdit()will return the inverse ofIsRunning(), except when the simulation has been paused, in which case both methods will returnfalse.IsResimulating
() → booleanReturns whether the client is currently in a resimulation step after a misprediction in the server authority model.Returns
trueif the engine is currently replaying simulation steps after detecting a misprediction in the server authority model; returnsfalseotherwise. When the server's authoritative state diverges from the client's predicted state, the client rolls back and rapidly resimulates all affected steps within a single frame. During that resimulation pass,BindToSimulation()callbacks and property-changed signals fire again for each replayed step, which can cause unwanted side effects such as duplicate sound playback or redundant network requests.IsRunMode
() → booleanReturns whether a Run playtest has been initiated in Studio.This method returns whether a Run playtest has been initiated in Studio. It will continue to return
trueif the simulation has been paused using the Pause button; however, once it has been stopped using the Stop button, it will revert to returningfalse. Note that Studio only enters "run" mode when a Run playtest (without the user's player character) is initiated. Also note that this method will returnfalseif the simulation was started usingRunService.Run.IsRunning
() → booleanReturns whether the experience is currently running.Returns whether the experience is currently running.
IsRunning()will always return the inverse ofIsEdit()except when the simulation has been paused, in which case both methods will returnfalse.IsServer
() → booleanReturns whether the current environment is running on the server.This method returns whether the current environment is running on the server. If the code that invoked this method is running in a server context (in a
ScriptwithRunContextset toRunContext.ServerorRunContext.Legacy, or in aModuleScriptrequired by aScript), this method will returntrue. In all other cases, this method will returnfalse.If this function returns true, then the current environment can access server‑only features like
ServerStorageorServerScriptService.IsStudio
() → booleanReturns whether the current environment is running in Studio.This method returns whether the current environment is running in Studio. It can be used to wrap code that should only execute when testing in Studio.
Pause
() → ()PluginPauses the experience's simulation if it is running, suspending physics and scripts.This method pauses the experience's simulation if it is running, suspending physics and scripts. When the simulation is paused,
IsRunning()will returnfalse.Run
() → ()PluginRuns the game's simulation, running physics and scripts.This method runs the experience's simulation (physics and scripts). When the simulation is running,
IsRunning()will returntrue. However,IsRunMode()will only returntrueif the simulation was started using the Run button in Studio.SetPredictionMode
(context: Instance, mode: PredictionMode) → ()Sets the prediction mode for anInstanceto anPredictionModevalue.Sets the prediction mode for an
Instance(context) to anPredictionModevalue. Mismatches between the physics properties or attributes for predicted instances will cause a rollback and resimulation on the client. Can only be called on the client.Stop
() → ()PluginStops the experience's simulation if it is running.This method stops the experience's simulation if it is running. When the simulation is stopped,
IsRunning()will returnfalseandIsEdit()will returntrue.In contrast to the Stop button in Studio, calling this method will not restore the experience to the state it was in prior to the simulation being run. This means any changes made to the experience by the physics simulation and scripts will persist after the simulation has ended.
UnbindFromRenderStep
(name: string) → ()Unbinds a function that was bound to the render loop usingRunService.BindToRenderStep.Given a name of a function sent to
BindToRenderStep(), this method will unbind the function from being called duringPreRender. This is used to unbind bound functions once they are no longer needed, or when they no longer need to fire every step.If there is no bound function by the given name, this method takes no action and continues without raising an error.
Events 9
Heartbeat
RunService.Heartbeat:Connect(function(deltaTime))Fires every frame, after the physics simulation has completed.The
Heartbeatevent fires every frame, after the physics simulation has completed. ThedeltaTimeargument indicates the time that has elapsed since the previous frame.This event is when most scripts run. It occurs at the end of each frame and it's also when any waiting scripts are executed, such as those scheduled with the
tasklibrary.Heartbeatis commonly used for periodic tasks, such as updating core game systems like health regeneration.Following this step, the engine sends property updates and events to the server or clients which are later received as part of the replication receive step.
Misprediction
RunService.Misprediction:Connect(function(time, instances, stats))In the server authority model, fires during prediction when the engine detects that the client has diverged from the server's authoritative state. Intended for plugin-based debugging.With server authority enabled, the Misprediction event fires when a received server state differs from the predicted state.
Use this event to build debugging tools that inspect misprediction data programmatically. For example, a plugin could listen for this event and track which instances are frequently mispredicting, what properties are diverging, and by how much.
Instances Array
Each entry in the
instancesarray is a table with the following structure:Stats Dictionary
The
statsdictionary contains the following fields:Code Sample
PostSimulation
RunService.PostSimulation:Connect(function(deltaTimeSim))Fires every frame, after the physics simulation has completed.The
PostSimulationevent fires every frame, after the physics simulation has completed. ThedeltaTimeSimargument indicates the time that the current frame has stepped the physics simulation, not accounting for physics throttling. This may deviate from the actual time between frames in Studio edit mode or when framerate is very low.This event is useful for making final adjustments to the outcome of the simulation. Following this phase, the engine triggers the
Heartbeatevent.PreAnimation
RunService.PreAnimation:Connect(function(deltaTimeSim))Fires every frame, prior to the physics simulation but after rendering.The
PreAnimationevent fires every frame, prior to the physics simulation but after rendering. ThedeltaTimeSimargument indicates the time that the current frame has stepped animations.This event is useful for modifying animation objects, such as adjusting their speed or priority. Once the
PreAnimationevent is complete, the engine proceeds to run these animations, updating the joint transforms which will later be used to update objects during the physics simulation.After animations are stepped, the engine triggers the
PreSimulationevent.PreRender
RunService.PreRender:Connect(function(deltaTimeRender))Fires every frame, prior to the frame being rendered.The
PreRenderevent (replacement forRenderStepped) fires every frame, prior to the frame being rendered. ThedeltaTimeRenderargument indicates the time that has elapsed since the previous frame.This event allows you to run code and update the world before it's drawn on a player's screen. This is useful for last‑minute adjustments such as changing object positions, updating animations, or preparing visual effects, but it should be used sparingly as the engine cannot start to render the frame until code running in this event has finished executing.
As
PreRenderis client-side, it can only be used in aLocalScript, in aModuleScriptrequired by aLocalScript, or in aScriptwithRunContextset toRunContext.Client.PreSimulation
RunService.PreSimulation:Connect(function(deltaTimeSim))Fires every frame, prior to the physics simulation.The
PreSimulationevent (replacement forStepped) fires every frame, prior to the physics simulation. ThedeltaTimeSimargument indicates the time that the current frame will step the physics simulation, not accounting for physics throttling. This may deviate from the actual time between frames in Studio edit mode or when framerate is very low.This event is useful for adjusting properties like velocity or forces just before they're applied as part of the simulation. The simulation then runs, potentially multiple times, as the physics solver runs at a higher frequency than other engine systems. Once this is complete, the
PostSimulationevent is fired.RenderStepped
RunService.RenderStepped:Connect(function(deltaTime))Fires every frame, prior to the frame being rendered.Fires every frame, prior to the frame being rendered.
Migration Note
This event has been superseded by
PreRenderwhich should be used for new work.Rollback
RunService.Rollback:Connect(function(time))In the server authority model, this fires after rolling back the predicted state due to a misprediction, but before resimulation begins.When server authority is enabled, the
Rollbackevent fires after the engine detects a misprediction and rolls back all eligible state (properties, attributes, and related physics and animation state), but before the engine resimulates the rolled-back steps.Use this event to update state in your place that wouldn't otherwise be rolled back after a misprediction. For example, if you maintain gameplay state that can't be stored in a property or attribute with simulation access, you can listen for this event, compare the rolled-back-to step against your farthest-reached step, and rewind your custom state accordingly.
Stepped
RunService.Stepped:Connect(function(time, deltaTime))Fires every frame, prior to the physics simulation.Fires every frame, prior to the physics simulation.
Migration Note
This event has been superseded by
PreSimulationwhich should be used for new work.
RunService also has every member of Instance. Deprecated and Roblox-only members are hidden.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.