In Reusing Code, you might have noticed frequent use of the game:GetService() method. Roblox services let you access the built-in features of the engine, like selling in-experience items, enabling chat, playing sounds, animating objects, and managing instances.
In fact, services are the first step in the most fundamental, common pattern of Roblox development:
- Get services.
- Require module scripts.
- Add local functions.
- Add the events that trigger those functions.
For example, you might want to save players' positions in the world when they exit your experience:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SaveManager = require(ReplicatedStorage:WaitForChild("SaveManager"))
-- Local function that calls a reusable function in the module script.
local function saveProgress(character)
-- Get the position of the player's character.
local position = character:FindFirstChild("HumanoidRootPart").Position
-- Use the saveData function in the module script, which writes to the
-- DataStoreService.
SaveManager.saveData(character, position)
end
-- Another local function that calls saveProgress() when a character is removed
-- from the experience (in this case, when the player leaves).
local function onPlayerAdded(player)
player.CharacterRemoving:Connect(saveProgress)
end
-- Calls onPlayerAdded when a player first connects to the experience.
Players.PlayerAdded:Connect(onPlayerAdded)
Some key details include:
- Because you should only retrieve a service once per script, the convention is to give the variable the same name as the service. This convention applies to module scripts, as well.
- You retrieve services with the global variable game, a reference to the root of the data model.
- Roblox doesn't make guarantees around loading order (and instance streaming further complicates what is and isn't loaded at any given time), so the use of Instance:WaitForChild() is an important safety measure.
Rather than comparing standard libraries, global functions and variables, or third-party libraries, a big part of Roblox development is identifying which of the many, many services can help you add the desired functionality to your experiences. In the example above, instead of using a standard I/O library to write to disk, you use cloud services to store data.
Container Services
Container services can contain and influence other objects. These container services reside at the root of the data model and are visible in Studio's Explorer window. Collectively, these container services form a structured hierarchy for the data model, so the Roblox engine can properly interpret and render your place. The following table includes some common container services.
Service | Description |
---|---|
Workspace | Contains all objects that render in the 3D world, such as parts and terrain. |
Lighting | Contains objects for setting universal lighting effects, such as Atmosphere and Sky. |
ReplicatedStorage and ReplicatedFirst | Contain content and logic that replicates between the server and client. |
To further examine the data model, you can use these methods:
- game:FindService() searches for the instance of the specified service.
- game:GetChildren() returns an array of all root children of the data model, which are the top-level container services.
- game:GetDescendants() returns an array of all the descendants of the data model, including all container services and their children.
For more information on container services, see the data model documentation.
Scripting Services
Scripting services provide standard functionality in the Roblox engine that you call within scripts. The following table includes some common scripting services.
Service | Description |
---|---|
TweenService | Used to interpolate numeric properties of other instances from a start to end value, with options for easing direction and style, repeat, and delay. |
MarketplaceService | The service responsible for in-experience transactions, such as prompting the player to purchase a developer product, subscription, or game pass, upgrade to Roblox Premium, etc. |
ContextActionService | Allows you to bind user input, such as a key press, screen tap, or controller button press to contextual actions, such as modifying controls when the user enters and exits a car. |
RunService | Contains methods and events for frame-by-frame time management, as well as for checking the context (server, client, Studio mode) in which the experience is running. Useful for running any process or update on every runtime frame. |
SoundService | Controls various global aspects of how audio plays in an experience, such as the doppler scale and volumetric audio. Can also contain sound groups to control the volume and dynamic effects properties of multiple audio signals at once. |
CollectionService | Manages groups (collections) of instances with tags that replicate from the server to the client, letting you more easily assign and work with groups of related instances. |
Cloud Services
Roblox also has special cloud services for handling tasks and processes that occur in the Roblox cloud. The following table includes some common cloud services.
Service | Description |
---|---|
DataStoreService | For storing persistent data across sessions. |
MemoryStoreService | For storing frequent and ephemeral data that change rapidly. |
MessagingService | For communicating between multiple servers during live sessions. |
Cloud services also have corresponding web APIs; they're accessible from external scripts or tools. For more information, see Open Cloud.