So I’m making a local script(inside StarterPlayerScripts) that needs to access a variable that is in a script in the workspace. I wanted to know how I could possibly do it since I’m new to scripting.
I’ve heard about _G variables but when I tried it, it didn’t work. I also read that Modules Scripts can be a good solution to access variables between scripts. But I have no idea how to do it. So I’d like to get some advices from more experienced scripters if possible. Thanks for helping
local RE = game.ReplicatedStorage.RemoteEvent
local function Input(input, gpe)
if IsKeyDown() then
CoreOn = false
RE:FireServer(CoreOn)
end
end
UserInputService.InputBegan:Connect(Input)
Server code (above while loop):
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, val)
CoreOn = val
end)
You probably can’t use ModuleScripts because module scripts can only be called once per script and they cache results. _G can only share variables among scripts with the same context (basically scripts of the same type, LocalScript or Script) and using _G is a bad practice
Module scripts are definitely a great option, they are really powerful. _G variables are powerful aswell but they can be seen as a bad practice sometimes depending on what you are using it for (you can read more about that here: Sharing variables between scripts? and What is _G and for what i can use it? ). Module scripts are scripts that when you require them it sends back a table of all the things inside the script. So for example:
local module = {
["Varible1"] = 5,
}
return module
in the module script above a table containing Varible1 is returned, but module scripts can be used to return any magnitude of things, some of the most common are functions
local module = {}
function module.Test()
print(10)
end
return module
in this case a function is returned that we can now call from any where we require it from!
This might help you get some more insight on stuff i mentioned: