Can you make custom global variables?

Hello, I would like to know if it is possible to make custom global variables instead of just being able to use _G. Example:

_X = 5

On another script:

print(_X)
1 Like

If you want to do this, you’ll want to use modules. It’s more widely accepted then _G, and seen as typically better code.

2 Likes

So would you do:

-- In a script
local X = 5
-- In a modulescript
local module = require(game.Workspace.Script)
print(X)

return module

More of the other way around. In a module, you would do the following:

local module = {}
module.X = 5

return module

And in a script,

local mod = require(TheModuleScriptThatIsSoCool)

print(mod.X) -- 5

Please note that scripts and module scripts are separate. Modules scripts come with the whole “local module = {} return module” shebang and you don’t really have to worry about that.

Additionally, if you wish, you can change the values in a modulescript from a script.

local mod = require(TheModuleScriptThatIsSoCool)

mod.X = 10

print(mod.X) -- 10
2 Likes

Do you have to call it “module” or can you name it

 PlayerHealth = {}

Also can you create custom variables?

Absolutely. Custom variables galore.

No, it doesn’t have to be named module, just make sure to actually return it. I’m not sure what you mean by “custom variables.”

OP Means custom variables under names he chooses.

“Health”
“Damage”
etc

1 Like

So to create a new variable would you write

local module = {}
module.newvariable = 2
print(module.newvariable)

Or would you write something else
Can you make more then one module like:

local m1 = {}
m1.X = 2
local m2 = {}
m2.Y = 7

I’ve never tested the “more then one module” thingymabob. I don’t see why’d you do that though. But might make for a fun experiment

Your right on the newvariable. Just make sure you return the module at the end of your code however, but that’s done for you automatically just as long as you don’t delete the code that’s there when you first create it.

What happens if you don’t use

return module

Will it throw an error or just not do anything?

Probably throw an error, or not do anything. Probably both. Never tested it. Either way, it wouldn’t work lol

I just tried what you said and it worked. Everything leading up to now helped me understand how module scripts work. Thanks!

1 Like

But why use module scripts when you could use _G which seems way simpler.

_G May be more inefficient, but the things for sure are:

It’s much more organized to use modules
_G Is old and pretty much on the verge of deprecation
_G Is seen as bad coding
_G Has a way of reeling in new coders due to its seemingly very simple nature

Modules are worth the “hassle” (even though there is none once you get it) and certainly much better over _G.

Why would Roblox need to deprecate anything people would just learn what they’re using is bad over time.

So people will stop using it much faster if Roblox deprecates it.

local module = 0

return module

To add onto this, a module can in fact return a single value of any primitive type (table, string, number, nil, boolean) however tables are most frequently returned. In the above example the module simply returns the number value 0, with a ModuleScript instance which contains that code some other script could do.

local module = require(pathtomodule)

print(module)
--0 is output

With this scarcely used convention of modules you have essentially created a global variable which doesn’t need to be indexed from a table value returned by the module beforehand.

local myGlobal = require(pathtomodule)

print(myGlobal) --0 is output
1 Like