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!
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.
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?