What are the downsides/problems that come with assigning a service to a global variable?

Sorry if my wording is weird, Im self taught so I dont know the exact correct terms to use. My question is as the title suggests I dont have enough experience with global variables to see the problem. I think it would be more organized for my scripts to contain the relevant variables rather than have to define the numerous services that I use.

2 Likes

The Script might just run slower with global variables, so it’s good practice for most developers to switch to local variables when defining the services they need.

The difference between

local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")

RunService.Stepped:Connect(...)

and

_G.RunService.Stepped:Connect(...)

is just that you don’t have to define the local variables to the services you want to use. Generally, it isn’t the best practice to use global variables, especially in a situation where its not something generated by a script (i.e. generating a value via a script that is crucial to many other scripts). Adding the service declarations at the top of your script should be fairly quick and simple; furthermore, you should not have other scripts rely on another script that may not work or run immediately.