Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why should I be using :GetService()?

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!

1 comment

0

This is not a spoon feed site

1 answer

✓ Accepted answer

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.

2 comments

0

So should I be using GetService for things like players and serverstorage or no?

3

It would be recommended.

This discussion is preserved as it was. New answers, comments, and votes are closed.