Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Is a variable only local to one script, or to the whole game?

Asked by 6 years ago

I am very curious, and your help would be greatly appreciated.

6 answers

Log in to vote
1
Answered by 6 years ago

It depends.. Variables can be local or whole game. To make a local one.. Just do this!

local var = game.Version -- Makes a variable and sets it.
game.Players.Name = var -- Can be used later.
var = "hi" -- Sets the var to something new.

Global variables aren't actually called "variables" they are called "values". Values can be made in studio or in a script and they can be accessed via the client and server unless they are in a server only or client only area.

Heres how to make one in studio!

local value = Instance.new("NumberValue") -- Create the value.
value.Name = "Hi" -- Set its name.
value.Value = 43 -- Set its value. Values can be changed on the server and client.
value.Parent = workspace -- Set its parent.

Now lets say you have another script.. You can do this!

game.Workspace.Hi.Value = 20 -- Sets the value to something new!

I hope this explained what they are! If it did please click the "correct" button! Have a nice day! c:

0
I think they meant global variables as in "foo = 1". hiimgoodpack 2009 — 6y
0
There is no correct button on the site. Please stop being a greedy person. User#19524 175 — 6y
Ad
Log in to vote
1
Answered by
T1mes 230 Moderation Voter
6 years ago

As the other answers said, variables are local to scripts if you're just doing:

var1 = 5

If you want to have global variables though, you may do:

_G.var1 = 6

this will allow you to do it throughout scripts in the server.

Log in to vote
0
Answered by
oreoollie 649 Moderation Voter
6 years ago

Roblox's variable setup is quite odd. The way RBX.lua is setup, variables declared local (using local) and variables declared "global" can only be access by the script they're within. Local variables are more efficient and since they are the same as global variables, there is literally never a reason to use a non-local variable or function. If you want a truly global variable, you will need to use the shared table _G. All scripts on the same end have the same _G table. By this I mean that the server has its own _G table and each client has its own _G table. It is not recommended to use the _G table for functions, it's best to use BindableFunctions if you want a global function. Below is an example of local vs global variables. Script 1:

local A = 1
B = 2

print(A) -- Output: 1
print(B) -- Output: 2
print(C) -- Output: nil
print(D) -- Output: nil

Script 2:

local C = 3
D = 4

print(A) -- Output: nil
print(B) -- Output: nil
print(C) -- Output: 3
print(D) -- Output: 4

Below is an example of using the _G table. Script 1:

local A = 1
_G.B = 2

print(A) -- Output: 1
print(_G.B) -- Output: 2
print(C) -- Output: nil
print(D) -- Output: 4

Script 2:

local C = 3
_G.D = 4

print(A) -- Output: nil
print(_G.B) -- Output: 2
print(C) -- Output: 3
print(D) -- Output: 4

I hope my answer was able to explain this to you best! If it was please remember to mark it as correct!

0
Stop being so desperate for reputation. There is no correct button. User#19524 175 — 6y
Log in to vote
0
Answered by 6 years ago

Using _G to make variables global to other scripts is not the best idea. You’re just filling up the _G table with things. Instead, you can use ModuleScript objects to get the same (and better!) effect.

-- ModuleScript

local foo = {}

function foo.bar()
    print"This was printed from a "..script.ClassName.."."
end

return foo

To call module scripts, there is a global function called require. You need to require your modules in order for them to execute code.

local module = require(game.Workspace.ModuleScript)

module.bar() -- prints whatever it prints in the function

Another thing to note about modules, is that they take on the behaviour of a Script or a LocalScript, depending on what you use. For example, if you used LocalPlayer in your ModuleScript, you’d have to require it from a LocalScript, else LocalPlayer will return nil.

Log in to vote
-2
Answered by
OBenjOne 190
6 years ago

Variables are local to one script. Ex:

If I put number = 1 wait () print (number) in one script and number = 2 wait () print (number) in another, in the output I would see both 1 and 2

Log in to vote
-2
Answered by 6 years ago

There is global and local variables. They are for the variables scope

Local is like this:

local LocalVar = 'This variable is local'

Global is like this:

LocalVar = 'This variable is global'

Hope this helped!

Answer this question