So whenever i use remote events I sometimes forget to say:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
And i just say:
game.ReplicatedStorage.RemoteEvent:FireServer()
So what is the point of using that variable?
So whenever i use remote events I sometimes forget to say:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
And i just say:
game.ReplicatedStorage.RemoteEvent:FireServer()
So what is the point of using that variable?
It’s used if the service is forked
What does forked mean? (30char)
Changed (Say name). Anything forked means that it is taken and edited
Some services aren’t named based on what kind of service they are. For example, UserInputService
is just named Instance; try printing game:GetService("UserInputService")
and see for yourself.
While that may not be true for other services, consistency is key. Having a mix of game.Service
and game:GetService(Service)
will make your code messy and hard to understand. Sticking to a consistent method when you can goes a long way for making your code readable.
Overall it’s just a preference thing, but I’d highly recommend using :GetService
for the times where it does matter.
GetService is highly recommended, however, not required.
This is mainly used for services that takes more time to load.
It has the advantage of creating the service if it hasn’t been created when calling it.
It also returns the classname of the service instead of its actual name.
I use:
local repStorage = game:GetService("ReplicatedStorage")
Whenever I just want to define something.
Then, it is easier down the road to do something like this:
repStorage.RemoteEvent:FireServer()
etc…
I would highly recommend doing this, but quoting @Xueify,
It is not necessarily required.
If you rename your services, you could use GetService()
instead to get the actual classname, which is very efficient if you ever renamed your services.
No, it is not required, as I already stated.
A lot of people may choose what to use.