3.2 Accessing Game Services

In an earlier section, you learned that the top level objects found in the Explorer are known as Services. Every object that exists in-game is a descendant of a service.

So in order to index something in the game, you must first index one of the many services.

We know that Workspace is a Roblox service so let’s grab that first. Let’s also index a few other services while we’re at it. The game provides the ‘GetService’ method to do just that.

Lua
local workspace = game:GetService("Workspace")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

The services are requested from the game object and then assigned to the variables workspace, replicatedStorage, and serverStorage.

game‘ is a Roblox defined global variable (do as I say, not as I do) referencing the game itself and is accessible from any script. This is the topmost container from which everything else belongs. Sometimes you might also hear it referred to as the ‘DataModel.’

Method‘ is a special type of function that belongs to a class or object and can only be called in the context of that class.

The print function you have been using is a sort of “global” function that can be called anywhere. But a method depends on data from its class or has its utility within that class and therefore constrained to it.

Most methods are called using the colon operator (:) followed with the parenthesis like you have been doing. We’ll talk more about methods when we get to object-oriented programming.


You may have noticed that the variable you named ‘workspace‘ is a different color than replicatedStorage and serverStorage.

Why did that happen?

This is because Roblox already has a global variable named ‘workspace.’ There is only one workspace and that is the very same workspace you just index using GetService.

The workspace is referenced often enough that Roblox set aside the name specifically for workspace. It’s fine to keep it as-is or you can change your variable name. All you’ve done is create a local variable that points to the global variable.

These are all valid ways to index the workspace:

Lua
local var1 = workspace
local var2 = game.Workspace
local var3 = game:GetService("Workspace")

In the second example, the dot between game and Workspace is called the ‘dot operator‘.

Recall that workspace and the rest of the services are [computer science] objects. In fact everything you can search and add from the object browser is an object.

The dot operator provides access to an attribute or function of an object. Additionally the dot operator can also be used to index a child object of some object.