Uses of :Getservice

this is something im not understanding very well.

what can this be used for and which are the services?

1 Like

There’s many uses of :GetService. You use it to have more possibilties in roblox studio.

The Roblox Developer website has API references (ServiceProvider:GetService()) which should explain it enough.

Returns a service with the class name requested. When called with the name of a service (such as Debris ) it will return the instance of that service. If the service does not yet exist it will be created and the new service is returned. This is the only way to create some services, and can also be used for services that have unusual names, e.g. RunService’s name is “Run Service”.

:GetService is used to get services from Roblox, such as MarketplaceService, DataStoreService and much more!

Here is an example:

local Service = game:GetService("DataStoreService")

I understand how it works (basically the same as game.Lighting but some services cannot be retrieved with a simple hierarchy).

What I don’t understand is why people use game:GetService("Lighting") when game.Lighting is wayyyy quicker.

3 Likes

Thats exactly how I think. Some people just like doing things different :man_shrugging:

think that about does it for me thx for the fast explanation.guess Ill have to study what each one does

1 Like

You use GetService() because it’s best practice. If you rename your services without using GetService(), your code will fail.

If you want to improve speed of this - as GetService() is a function call - you can cache the result. However, since you’re calling it once to initialize a value, it really isn’t a massive performance impact at all.

You should always use GetService() to make sure your code isn’t prone to fail due to a service being renamed or cannot be indexed by name as some services don’t exist by default - so using GetService() actually will create the service such as the Teams service.

9 Likes

What do you mean by “rename my services”? Can you “rename” workspace etc? Plus, I said that not all services can be accessed with game.Service, but why use :GetService with Lighting or ServerScriptService?

Change the name. You can change the name of the services lol

Yes, you can easily rename a service.

workspace.Name = “hello world”

Also, as I said before, you’re usually only doing this once to initialize a value so, for best practice you would actually do:

local lighting = game:GetService(“Lighting”)

lighting.Whatever = whatever

This is to make sure your code is not going to error due to any changes made to the service. It’s especially important if your code is going to run on other people’s games, as you don’t know if they’ve renamed their services or not.

I’m on mobile, please don’t kill me for my half-effort code example lol

3 Likes

Sorry, pls not mean this bad. Its normal that we all learn something always even if you think that you know everthing. Sorry for this if you meant this bad, if you want i can will delete it.

For example, lets say i change the name of the Lighting Service from „Lighting“ to „LeuchtDing“ (this is german, if you are bad at english for example, it is the same as „Lightting“) but because you are using this code:

local Lighting = game.Lighting

This will now error.

How to solve this? This is why the :GetService() function exist

Hope this helped.

4 Likes