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

I have a question about WHY :GetService is used, not how, please help xD?

Asked by 5 years ago

Alright so I have learned that there are "services" in studio. But why is it used?

local folder = game:GetService("ReplicatedStorage"):FindFirstChild("Folder")
 local subFolder = folder:FindFirstChild("Folder")

Alright so I think I get this code for the most part... So is the first line saying that a folder is equal to a folder in the replicated storage? If so, why do u have to use :GetService???

1 answer

Log in to vote
0
Answered by
valchip 789 Moderation Voter
5 years ago
Edited 5 years ago

Got this info from another question.

Link here

Most services are direct children of game. However, not all of them are, and some might not exist when your game is run. In those cases, you need to use GetService to access those services. We can simplify down GetService and say it has two uses:

The first use is getting a hold of services when you cannot access it through game.ServiceName. For instance, UserInputService is not a child of game, so whenever you want to use it you must access it through game:GetService("UserInputService"). I would also be unable to access the lighting through game.Lighting if I had renamed it to something other than "Lighting". If a service has had its name changed and your script doesn't know what the new name is, the only way you can access it is through GetService.

The second use is for loading services that are not currently loaded. The only services I know aren't loaded by default off the top of my head are TestService and Teams, but there are probably more. In TestService's case, you need to use game:GetService("TestService") to create it. You also might have a service that somehow gets deleted and you need to re-create it.

The wiki has a list of all services if you're interested in how many there are and what they do. At the bottom of the page, all of the items under "Services" are services, and some under the "Containers" section are services as well. Services are used very frequently in scripting, from prompting the player to purchase items to reading input from their keyboard and mouse, so you need to make sure you know how to use them. You've probably already used some without knowing though. Workspace, Lighting, and Players are all services. Invoking game.Players:GetPlayers() is using a service.

Also click here for wiki's article.

Credits to EchoReaper

0
Yo this is sooo helpful. That link to the service list did not work. Thanks mate! I would vote this up if I could lol hawkeye1940 8 — 5y
0
I realize you gave 'credits' though you could just paste the link (https://scriptinghelpers.org/questions/22782/what-does-getservice) in a comment, this is kinda stealing someone's answer User#20388 0 — 5y
Ad

Answer this question