New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
0

# Should I use game.Players or game:GetService("Players")?

Edited 3 months ago

I have used Roblox Lua some time, and in every script I ever write, when I want to access, let's say, Players, I do

local players = game.Players


However, I have frequently seen others use

local players = game:GetService("Players")


instead. Is this "better" or more efficient in any way? To me, it just seems a bit more complicated, and I only use :GetService() for Services like TweenService or DebrisService, which aren't found in the Explorer.

1
BashCaster 2370
3 months ago

game.Players indexes a child of game named Players. If, for some reason, you renamed the Players service, game.Players will either index a nil value or, if a different child has the name Players, it will index that child. Thus, GetService() is considered the standard go-to for referencing services. GetService(serviceName) returns the service with the name serviceName. For every service (except the Workspace and any service that can't be retrieved), GetService() is a MUST. (You can do game:GetService("Workspace"), although doing so is mostly irrelevant because of the workspace shared variable.)

0
Thanks, this helps. so, in other words, don't use 'game.Players', and use 'workspace' instead of 'game.Workspace', right? RiskoZoSlovenska 239 — 3mo
1