34. Services and the data model
Everything in a Roblox experience hangs off one root object called
game — the data model. Directly under it
sit a fixed set of big containers called services:
Workspace holds the 3D world, Players the
people, ServerScriptService the server code. This chapter
maps that top level, so you always know where to reach.
game and the services
game is the top of the tree from Chapter 32, and its
children are services. You never create them — they always exist. The
ones you meet first:
| Service | Holds |
|---|---|
Workspace |
The 3D world: parts, models, the baseplate. |
Players |
One Player object per connected person. |
ServerScriptService |
Server Scripts. Players cannot see inside. |
ReplicatedStorage |
Things shared between server and players. |
StarterGui |
The on-screen interface each player starts with. |
Lighting |
Time of day, fog, and lighting effects. |
Getting a service:
game:GetService
Reach a service reliably with
game:GetService("Name"):
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")game.Players also works, but :GetService is
the form Roblox docs use, and it is safer — it finds the service even
where the plain dot form might not. Make it a habit at the top of every
script.
workspace is special: it has its own lowercase global,
so workspace means the same as
game:GetService("Workspace").
Where code lives: server and client
Roblox runs your experience in two places at once, and where a script sits decides where it runs:
- A
ScriptinServerScriptServiceruns on the server — the shared computer hosting the game for everyone. Use it for anything that affects the whole world: spawning coins, keeping score. - A
LocalScriptruns on a single player's device. Use it for what only that player sees: their screen, camera, and buttons. - A
ModuleScriptis the shared module from Chapter 25. It runs nowhere by itself; other scriptsrequireit.
This book's projects use a server Script, since they
change the world for everybody.
Reaching across the tree
From any script you can walk from game to anywhere. A
few patterns you'll reuse:
local Players = game:GetService("Players")
-- every player currently connected, as a list
for _, player in ipairs(Players:GetPlayers()) do
print(player.Name)
end-- a shared value other scripts can read, kept in ReplicatedStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local roundTime = ReplicatedStorage:FindFirstChild("RoundTime")Players:GetPlayers() returns a list you loop with
ipairs, like any list from Chapter 22. Underneath, the tree
is the same nested tables from Chapter 23 — services are just the top
branches.
Homework
Written exercises. Paste into a Script in
ServerScriptService to try.
Problem 1 — Get the services
Open exercises/34/homework/01-get-services.lua. At the
top of a script, get the Players and
ReplicatedStorage services into two locals with
GetService.
Problem 2 — List the players
Open exercises/34/homework/02-list-players.lua. With
Players:GetPlayers() and an ipairs loop, print
the Name of every connected player.
Problem 3 — Server or client?
Open exercises/34/homework/03-where.lua. In a comment,
answer: for code that spawns a coin for everyone, would you use a
Script in ServerScriptService or a
LocalScript? Why?
Challenge — Count the players
Open exercises/34/homework/04-count-players.lua. With
Players:GetPlayers() and the count pattern from Chapter 20,
print how many players are currently connected.
Stuck or finished? Open the homework solutions page.