Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

Why should I be using :GetService()? [closed]

Asked by 5 years ago

Whenever I read scripts written by other people sometimes they do something like this:

--[[ Services ]]--

players = game:GetService("Players")

and I am just wondering if I should be using GetService more often. Because what I generally do is:

game.Players.PlayerAdded:Connect(function()

Could someone explain the difference and tell me what I should be doing? Thanks!

0
This is not a spoon feed site tictac67 96 — 5y

Locked by User#19524

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
9
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
5 years ago

GetService() is a DataModel, or game, function that prompts the game to get the service you are requesting. Generally, the game doesn't have services visible in the explorer such as UserInputService, BadgeService, or PointsService (just to name a couple) and does not have them accessible to the game by default. What GetService will do is get the service that can't easily be referenced or gets the already existing service.

Back to the service that can not easily be referenced note, that is a strong suit for GetService. Back when FilteringEnabled wasn't widely used and exploits were strongly prominent, the GetService function would end up saving the running scripts. There would be exploits that would change the names of Services and, for those that relied on the service names like Players, Workspace, or Lighting not changing, scripts could still end up running when not referenced earlier in the script.

With FilteringEnabled and Roblox implemented exploit protections, the need to use GetService on services like the few I have mentioned in the last paragraph is not strongly required but is still recommended nonetheless to avoid conflict.


Hopefully this answered your question and if it did don't forget to hit the answer button. If you have any other questions, feel free to leave them in the comments.
0
So should I be using GetService for things like players and serverstorage or no? User#21908 42 — 5y
3
It would be recommended. M39a9am3R 3210 — 5y
Ad