Hello, so I have come to ask about the Shared Variable. From my understanding it’s similar to a _G variable, so would it be smart to utilize it in a large mainframe including 30-40+ module scripts in order to share information between each module scripts. E.G (Variables using the global so I don’t have to repeat them in each module. Each module reuse them multiple times.
You can define your variable(s) inside a modulescript. This module script can be require()
by other scripts to access the modules’s variables.
Yes I understand that but I have a mainframe using purely module and local scripts, the module scripts all using alot of the same variables. Could I use the shared variable to make a module script to hold the variable for the rest to use? It would also make editing the mainfraim easier.
Of course you can. You can also set variables inside the modulescript.
Thanks! I will be trying to implement global variables then
Using _G
(global) variables is generally not encouraged. This is because if you set a global value, it just hangs around in the global environment waiting to be used. Instead, storing shared variables inside a modulescript is a better option because you only access the variables when you need them.
Ye, but you can’t require a module script in a module script, and i have like 12-15 module scripts alone using proximity services, gets annoying to update each one
But you can require a module script in a module script, multiple community modules do this like fastcast and my ones, something smells here.
How do you require a module in a module? Everytime i try to, the variables stay in the original module and i have to rewrite the variables.
--Script
require(Module1)()
--Module 1
return function()
return (require(Module2)())
end
--Module 2
return function()
print("sup")
end
--Output
>>sup
Well, you cannot exactly transfer locally defined variables like:
local speed = 5
in one script to another script, we gotta use tables to store the data like in a dictionary to represent the variables or settings of a module something like this:
local gunSettingsModule = {}
gunSettingsModule.Name = "pistol"
function gunSettingsModule:GetName()
print(self.Name)
end
return gunSettingsModule
--Another module
local module2 = {}
local gunSettings = require(gunSettingsModule)
print(gunSettings.Name)--pistol
gunSettings:GetName()--pistol
return module2