RunService

RunService : Instance

Overview

The RunService class is a service that manages the execution of tasks and events tied to the runtime loop of a game. It provides methods and events for time management, frame-by-frame updates, and determining the context (server, client, or Studio) in which a script is running. Developers commonly use RunService for creating smooth animations, controlling game physics, and performing actions that need to occur every frame or at specific times.

Description

The RunService class is central to managing the runtime behavior of a game. It allows developers to hook into the game's frame-by-frame loop and execute code at different stages of each frame.

Key Features

  • Frame-Based Events:

    • RunService provides several events that fire at different stages of each frame:

      • RenderStepped: Fires before rendering on the client, ideal for camera manipulation or UI updates.

      • Stepped: Fires before physics simulation on both client and server.

      • Heartbeat: Fires after physics simulation on both client and server.

  • Context Detection:

    • Methods like IsClient and`` ``IsServerallow scripts to identify whether they are running on the client, server, or in Roblox Studio. This is particularly useful for shared ModuleScripts.

  • Task Scheduling:

    • Developers can use RunService as an alternative to infinite loops (while true do) for tasks that need to run continuously without risking script exhaustion.

Properties

ClientGitHash

string

{Description Slot}

Code Samples

Methods

IsClient

The IsClient method checks if the current execution context is running on a client.

Parameters

Return

bool

Code Samples

local RunService = game:GetService("RunService")

local IsClient = RunService:IsClient()
print("IsClient : ", IsClient)

IsServer

The IsServer method checks if the current execution context is running on server.

Parameters

Return

bool

Code Samples

local RunService = game:GetService("RunService")

local IsServer = RunService:IsServer()
print("IsServer : ", IsServer)

IsStudio

The IsStudio method checks if the current execution context is running in OVERDARE Studio. This is useful for debugging purposes or when specific actions should only occur during testing in Studio, and not in live gameplay.

Parameters

Return

bool

Code Samples

local RunService = game:GetService("RunService")

local IsStudio = RunService:IsStudio()
print("IsStudio : ", IsStudio)

Events

Heartbeat

The Heartbeat event fires after the physics simulation step and before the frame is rendered. It is commonly used for tasks that need to run every frame after physics calculations are complete, such as applying the results of physics simulations or performing game logic updates that should precede rendering.

Example uses include updating non-physics game mechanics, animating game objects, or performing real-time computations that depend on the latest state of the game's physics engine.

Parameters

Code Samples

local RunService = game:GetService("RunService")
local Timer = 0

local function UpdateEvent(deltaTime)
    Timer = Timer + deltaTime
    print(Timer)
end
RunService.Heartbeat:Connect(UpdateEvent)

Stepped

The Stepped event fires before the physics simulation step on both the client and the server. It is typically used for tasks that need to occur before physics calculations, such as preparing game state or performing updates that should influence the upcoming physics simulation.

This event can be useful for precise control over pre-physics updates or when synchronizing gameplay systems with the physics step.

Example uses include implementing custom movement logic, updating forces applied to physics objects, or adjusting player inputs before physics calculations begin.

Parameters

Code Samples

local RunService = game:GetService("RunService")

local function OnStepped(timeElapsed, deltaTime)
    print(timeElapsed)
end
RunService.Stepped:Connect(OnStepped)

RenderStepped

The RenderStepped event fires before the frame is rendered on the client. It is primarily used for tasks that need to run every frame and are specific to the rendering process, such as updating the camera's position, animating UI elements, or making visual adjustments to the game's appearance.

By using RenderStepped, developers can ensure that updates occur just before rendering, ensuring smoother visuals and better synchronization with the frame rate. It is worth noting that this event only fires on the client and is not available on the server.

Parameters

Code Samples

local RunService = game:GetService("RunService")
local Timer = 0

local function OnRenderStepped(deltaTime)
    Timer = Timer + deltaTime
    print(Timer)
end
RunService.RenderStepped:Connect(OnRenderStepped)