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 Roblox'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
Environment | IsStudio | IsClient | IsServer | IsEdit | IsRunning | IsRunMode |
---|---|---|---|---|---|---|
Live Player | false | true | false | |||
Live Server | false | false | true | |||
Edit Mode | true | true | true | true | false | false |
Collaborative Edit | true | true | false | true | false | false |
Run Mode | true | true | true | false | true | true |
Play Mode (Client) | true | true | false | false | true | false |
Play Mode (Server) | true | false | true | false | true | true |
Team Test (Player) | true | true | false | false | true | false |
Team Test (Server) | false | false | true | false | true | false |
Summary
Properties
Methods
Given a string name of a function and a priority, this method binds the function to RunService.PreRender.
Returns whether the current environment is running on the client.
Returns whether the current environment is in Edit mode.
Returns whether the Run button has been pressed to run the simulation in Studio.
Returns whether the experience is currently running.
Returns whether the current environment is running on the server.
Returns whether the current environment is running in Studio.
Pauses the experience's simulation if it is running, suspending physics and scripts.
Runs the game's simulation, running physics and scripts.
Stops the experience's simulation if it is running.
Unbinds a function that was bound to the render loop using RunService:BindToRenderStep().
Events
Fires every frame, after the physics simulation has completed.
Fires every frame, after the physics simulation has completed.
Fires every frame, prior to the physics simulation but after rendering.
Fires every frame, prior to the frame being rendered.
Fires every frame, prior to the physics simulation.
Fires every frame, prior to the frame being rendered.
Fires every frame, prior to the physics simulation.
Properties
ClientGitHash
RunState
Methods
BindToRenderStep
Parameters
Returns
Code Samples
local RunService = game:GetService("RunService")
-- How fast the frame ought to move
local SPEED = 2
local frame = script.Parent
frame.AnchorPoint = Vector2.new(0.5, 0.5)
-- A simple parametric equation of a circle
-- centered at (0.5, 0.5) with radius (0.5)
local function circle(t)
return 0.5 + math.cos(t) * 0.5, 0.5 + math.sin(t) * 0.5
end
-- Keep track of the current time
local currentTime = 0
local function onRenderStep(deltaTime)
-- Update the current time
currentTime = currentTime + deltaTime * SPEED
-- ...and the frame's position
local x, y = circle(currentTime)
frame.Position = UDim2.new(x, 0, y, 0)
end
-- This is just a visual effect, so use the "Last" priority
RunService:BindToRenderStep("FrameCircle", Enum.RenderPriority.Last.Value, onRenderStep)
--RunService.RenderStepped:Connect(onRenderStep) -- Also works, but not recommended
local RunService = game:GetService("RunService")
local function checkDelta(deltaTime)
print("Time since last render step:", deltaTime)
end
RunService:BindToRenderStep("Check delta", Enum.RenderPriority.First.Value, checkDelta)
local RunService = game:GetService("RunService")
-- Step 1: Declare the function and a name
local NAME = "Print Hello"
local function printHello()
print("Hello")
end
-- Step 2: Bind the function
RunService:BindToRenderStep(NAME, Enum.RenderPriority.First.Value, printHello)
-- Step 3: Unbind the function
RunService:UnbindFromRenderStep(NAME)