Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Data Stores and Services

Services and GetService: The Tools Built Into Roblox

Meet the Roblox services you will use daily, learn why you call game:GetService, and see where each one belongs, from Players and Workspace to RunService and Debris.

Roblox gives you a large toolbox that is ready to use in every script. Each tool is a service: a single, always-available object responsible for one area, such as players, sound, saving data or selling items. You will use services in nearly every script you write, so it pays to know the common ones and the right way to reach them.

The Roblox calls are shown read-only here. The runnable examples are JavaScript stand-ins that model the pattern of fetching a unique service by name, so you can feel how it works.

game:GetService

Services are children of the global object game. You could write game.Players, but the recommended way is GetService:

LUA
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

There are three good reasons:

  1. GetService creates the service if it does not exist yet, so it never returns nil by accident.
  2. It uses the real class name, so renaming a service in the Explorer cannot break your script.
  3. Studio autocompletes the name and the methods, which catches typos.

By convention you store the result in a local variable named after the service, near the top of the script.

The services you will meet first

Service What it does
Players The list of players, PlayerAdded and PlayerRemoving, and LocalPlayer on the client
Workspace The 3D world. Parts must be here (or inside a model here) to exist physically
ReplicatedStorage Shared storage that server and clients can both read
ServerStorage Server-only storage for templates and secrets
ServerScriptService Where server Scripts normally live
StarterGui, StarterPack, StarterPlayer Templates copied to each player
Lighting Time of day, brightness, sky and atmosphere
RunService Frame events: Heartbeat, RenderStepped, and checks like IsServer()
Debris Automatically removes objects after a delay
CollectionService Tags instances so you can find groups of them
UserInputService Keyboard, mouse, touch and gamepad input (client)
TweenService Smooth animations of properties
DataStoreService Saving and loading data on the server
MarketplaceService Selling gamepasses and products
TeleportService Moving players between places

Services by job

People Players, Teams, Chat.
World Workspace, Lighting, Terrain.
Data DataStoreService, MarketplaceService.
Tools RunService, TweenService, Debris.

Every service is one object, created for you.

1 / 2

Services in action

A few patterns you will write constantly:

LUA
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	print(player.Name .. " joined. Players now:", #Players:GetPlayers())
end)
LUA
local RunService = game:GetService("RunService")

-- Runs every frame, after physics. dt is the seconds since the last frame.
RunService.Heartbeat:Connect(function(dt)
	spinner.CFrame *= CFrame.Angles(0, math.rad(90) * dt, 0)
end)

Multiplying movement by dt makes it frame-rate independent. A device running at 30 frames per second and one at 144 turn the spinner the same amount per real second.

LUA
local Debris = game:GetService("Debris")

local effect = Instance.new("Part")
effect.Parent = workspace
Debris:AddItem(effect, 3) -- removed after 3 seconds

Debris is simpler and safer than task.wait(3) followed by Destroy() because it does not hold a thread open.

CollectionService lets you tag objects in the Studio Tag Editor (or in code), then react to every tagged instance:

LUA
local CollectionService = game:GetService("CollectionService")

local function setupDoor(door)
	door.Touched:Connect(function() print("door touched") end)
end

for _, door in CollectionService:GetTagged("Door") do
	setupDoor(door)
end
CollectionService:GetInstanceAddedSignal("Door"):Connect(setupDoor)

This is how large games control hundreds of identical objects with one script.

Which side can use which service?

Some services work only on the server, some only on the client, and some on both.

  • Server only: DataStoreService, ServerStorage, ServerScriptService.
  • Client only: UserInputService, ContextActionService, StarterGui calls for the local player's UI.
  • Both: Players, Workspace, ReplicatedStorage, TweenService, RunService (with IsServer() and IsClient() to tell them apart).

If a service call errors with "can only be called from the server", move that code into a Script.

Runnable stand-ins

The first example models GetService: a registry that creates each service once and returns the same object afterwards.

JS GetService registry (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A typo in a real GetService call also raises an error right away, which is exactly why it beats the dot syntax.

The second example models Heartbeat and delta time. It runs the same spinner at two different frame rates.

JS Frame-rate independent movement

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The correct version gives 180 degrees on both devices. The fixed-step version spins faster on the fast device, which is a classic bug.

Common mistakes

  • Using game.Workspace or game.Players instead of GetService. It usually works, but it is less safe.
  • Calling GetService inside a tight loop. Cache the result in a local variable at the top.
  • Using server-only services on the client, or the reverse.
  • Forgetting dt in per-frame code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is game:GetService("Players") preferred over game.Players?

    GetService is more robust because it asks for the service by class name, and Studio can check the name for typos.
  2. 2. Which service removes an object automatically after a delay?

    Debris:AddItem(instance, seconds) destroys the instance later without keeping a thread waiting.
  3. 3. Why multiply movement by dt in a Heartbeat handler?

    dt is the seconds since the last frame. Using it makes motion independent of frame rate.
  4. 4. Which of these can only be used on the server?

    DataStoreService is server-only. UserInputService is for the client, and TweenService and Players work on both.

Summary

  • A service is a built-in object that handles one area of Roblox; fetch it with game:GetService("Name").
  • Cache each service in a local variable at the top of the script.
  • Know the common ones: Players, Workspace, ReplicatedStorage, ServerStorage, RunService, Debris, CollectionService, DataStoreService.
  • Some services are server-only or client-only; RunService:IsServer() tells you where you are.
  • Use dt from Heartbeat for smooth motion on every device.

Next: TweenService: Smooth Animations