How can you get services for all scripts?

In many of my server scripts I have to constantly add a local variable to hold game:GetService() for everything I need to use it for such as getting tween service or run service or replicated storage. Throughout this use over time I started worrying about performance in the long run and am asking today if this is the best way to get all services. In my mind, I have thought about simply using global variables to store the services like _G.Tween = game:GetService(“TweenService”) but Im not sure if this is something that should be used at all or even if it works. Im asking for a very straight answer and if it possible to stop adding so many variables to know the solution as well.

This honestly isn’t a problem performance wise, because it takes only fraction of a millisecond to get a service, and you only need to define your services once at the start of a script.

Using a global variable would probably be worse in efficiency, because it takes slightly longer to get information from a module, than it does to use the . or GetService() methods.

This is a good question though!

  • GetService will have basically no performance impact on your code
  • Local variables are the fastest method of accessing variables
  • Accessing a variable from _G is going to be just as slow as calling GetService
  • It’s just boilerplate code you’re going to put at the top of a lot of scripts :slight_smile:

Thanks! Its definitely nice knowing that lag is minimized. Now whenever I code anything I get pretty nervous on performance. I didn’t know if this was something that everyone did or there was another way to do so

1 Like