Skip to Content
Kinemium
Services

Services

Get the engine's built-in services from game and use them to drive players, physics, input, and more.

Services are long-lived objects the engine creates for you. There's one of each, they're created the first time something asks for them, and they stay alive for the whole session. Instead of constructing a Players or a RunService yourself, you ask game for it.

game is the data model: the root of the object tree and the entry point to every service. Services are parented to it, and their Name matches the service name you use to look them up.

Getting a service

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

print(Players.Parent == game) -- true

GetService() returns the same object every time, so it's cheap to call where you need it rather than threading a service through every function.

You can also read a service like a property:

local Workspace = game.Workspace

This works only for registered service names and returns the same object as GetService(). GetService() is still the better habit, because a typo raises an error instead of quietly returning nil, and because the property form isn't available for every name on every platform.

When a service might not exist

GetService() raises an error if the name isn't registered, if the service can't be created, or if your script doesn't have the security capabilities the service requires. If you're checking whether an optional service is present, use FindService() instead:

local profiler = game:FindService("ProfilerService")
if profiler then
    print(profiler.Name)
end

FindService() returns nil in all of those cases rather than raising. Both methods are also exposed as properties (game.GetService, game.FindService) so you can pass them around, but call them with game as the receiver.

Availability depends on where the game runs. Some services are only registered on desktop builds, and a few are reserved for internal editor or scripting contexts; asking for those from a normal script raises an "insufficient security capabilities" error. Builds for the browser register a smaller set of services, with stand-ins for the ones the editor UI needs.

Closing the game

BindToClose() registers a function to run when the game shuts down. Use it to save state or release resources that won't survive process exit.

game:BindToClose(function()
    print("Shutting down")
end)

The callback receives no arguments and runs once for each registration. Registering the same function twice runs it twice. Registration lasts for the rest of the session; there's no corresponding unbind method.

Game metadata

game exposes a few properties describing the running session:

PropertyTypeCurrent value
PlaceIdnumber0
PlaceVersionnumber1
GameIdnumber0
JobIdstring""
PrivateServerIdstring""
PrivateServerOwnerIdnumber0

These are currently placeholders, so don't treat them as unique identifiers for a running server.

Documented services

ServiceWhat it's for
CollectionServiceTag instances and subscribe to tag changes.
ContextActionServiceBind named actions to inputs with priorities.
DebrisDestroy instances automatically after a delay.
GuiServiceRead platform inset and window details.
LightingSet the time of day, ambient light, shadows, and default effects.
LogServiceWrite structured log messages and read the history back.
PhysicsTune gravity, substeps, and sleep behavior.
PlayersWork with player objects, characters, and player events.
ReplicatedFirstHold content clients need early in startup.
ReplicatedStorageHold content both sides can access.
RunServiceQuery the runtime mode and run code on frame and simulation steps.
ServerScriptServiceHold server-side scripts.
ServerStorageHold server-only content.
StarterGuiHold screen interface templates.
TextServiceMeasure text with the engine's fonts.
TweenServiceAnimate numeric, UDim2, and Color3 properties over time.
UserInputServiceRead keyboard, mouse, and touch state and listen for input.
WorkspaceManage the 3D world, physics settings, camera, and raycasts.

More services will appear here as their Luau APIs are verified. In the meantime, GetService() works with any name the engine registers.

See also

  • Instance, the object tree services live in.
  • Object, shared behavior for every engine object.

On this page