Habitually, I pretty much always use :GetService()
.
I only know that it's conventional to always use the function :GetService()
, and not the particular reason why. I've looked at posts that people have made asking the same question, but the answers seem to be all over the place.
Is :GetService()
only used with services that aren't already given to you in the explorer?
Or is it just used as a precautionary measure to load the service if it hasn't loaded already, regardless of whether or not it's a service already given to you in the explorer?
The reason I want to know this is because simply indexing a service without using the function involves a lot less typing.
A simple search reveals an devforum post similar to this.
GetService is mainly used for services that aren’t guaranteed to be instantiated, like Teams and InsertService. For Players, it is fine to not use it, but you may get one complaint from an error “Players is not a valid member of game” because they have the Players service renamed if someone who doesn’t know what they are doing uses your code. If it is private code and Players isn’t renamed, there is no difference.
link to original post
GetService(ServiceName)
is similar to game.ServiceName
..
:GetService()
can be used with any service. As you said, it's actually better than saying game.Players
or whatever your service is called. It's better since it checks if the service isn't nil
. If it is, it makes/gets the service. To answer your question, yes. They can be used with any service you want.