RunService

RunService can be used for animation. It also allows handling real-time updates and frame-by-frame control.

EventsDetails
HeartbeatFires every frame after the physics simulation has completed.
PostSimulationFires every frame after the physics simulation has completed.
PreAnimationFires every frame prior to the physics simulation but after rendering.
PreRenderFires every frame prior to the frame being rendered.
PreSimulationFires every frame prior to the physics simulation.
RenderSteppedFires every frame prior to the frame being rendered.
SteppedFires every frame prior to the physics simulation.

RunService vs wait()

local part = Instance.new("Part")
part.Size = Vector3.new(4, 4, 4)
part.Position = Vector3.new(0, 5, 0)
part.Anchored = true
part.Parent = workspace

local speed = 5 -- studs per second

spawn(function()
    while true do
        wait(0.033) -- Approximate wait time for 30 FPS (Frame per second)
        part.Position = part.Position + Vector3.new(speed * 0.033, 0, 0)
    end
end)
wait() is less precise and can vary slightly in timing, leading to less smooth animations.

RunService vs TweenService

  • Ideal for smooth, predictable animations.
  • Best for simple property transitions.
  • Easy to use with built-in easing styles.
  • TweenService leverages mechanisms similar to what RunService provides to ensure smooth animations, including the use of delta time for frame-independent interpolation of properties.
  • Suitable for real-time, dynamic updates.
  • Allows frame-by-frame control.
  • Versatile for various tasks beyond animations.
  • RenderStepped is tied to the rendering process, which occurs on the client side, it is restricted to LocalScripts.

RunService Use Case Example

ExamplesDetails
Custom Render Logic3D GUIs: Create and update 3D GUI elements that must follow parts or characters in the game world.
Player-Exclusive FeaturesLocal Game Mechanics: Implement features or mechanics that should only affect the local player, such as local-only particle effects or lighting changes.
Camera ManipulationDynamic Camera Control: Smoothly update the camera's position, orientation, or focus based on player actions or game events to create a more immersive experience.