**Scripting Questions**

So I have some questions in the script world. I’m very new so sorry if I dumb or something like that.

1. game:GetService(“…”)
So is there any advantage of doing GetService method for services like ReplicatedStorage or Players?

2. Local/Global Functions
So Global functions can be used in other scripts so that’s the advantage. But in cases where you don’t need Global Functions I see people use Local Functions. Is there any advantage to that?

3. How to use Global Variables
So I was wondering how you would set up and use this part of scripting.

Thanks for all time you spent reading this and thanks for helping!

Tomroblox54321

1 Like
  1. You should use GetService for all services (except for workspace) instead of game.AService because it’s better for maintainability but all the functions for the services stay the same.

  2. You should learn about scopes and it will help you understand when you should use global and local variables: Scope.

  3. You should learn about basic scope and variables to learn how to use global variables: Variables

If you have any more questions, please ask and if I don’t respond, you can always search things up.

Thanks for replying this helps me a lot!

Im not sure about this, a global function can be used anywhere in the script it was written in, not any script located in your game

I think he means variables

example

local variable example:

local yes = true

global varible example:

_G.yes = true
2 Likes

Local variables are obtained faster than global variables because they’re integrated into the environment in which they were created. If possible, you should always use local variables over global variables, unless there’s a specific reason otherwise.

The same applies for locally defined functions as well. After all, function definitions are essentially the following.

local aFunc
aFunc = function(parameters)
end

The format you’re likely more familiar with.

local function aFunc(parameters)
end

Is just a syntactic sugar of the other format.

Thanks for helping @Katrist, @Forummer, @rorystxr and @Pixelctrl

1 Like