How do I use _G in a local script

Im work with a module script that creates a _G and I have a local script that need to access said _G. The variables set up this this:

main.folder = folderLocation

_G.Name = main

The local script that needs to access it is:

local Folder = require(_G.Name.folder.Module)

Is there something wrong with my code? Or can I not do this?

You don’t use _G. Using _G with modules defeats the purpose of modules, just use modules.

2 Likes

If you want to read from the value of a module, simply do:

print(require(module).nameOfCake)

Module Script (Called by server)

main = {}
main.folder= Instance.new("folder")

_G.Name = main

Local Script (Accessing _G)

local Folder = require(_G.Name.folder.Module) --Module is inside of the folder

You dont understand. _G is a global variable set by a module script and i need a local scrip to access it

Did you require it? Your question is really confusing, modules run in the environment they are required, so a module required by a localscript runs in a localscript environment and follows the rules that localscripts follow, otherwise if by a server script, then it follows the rules server scripts follow. If you avoid _G you will save yourself from some headaches. Why do you need _G anyway.

I never seen a professional developer uses this, I think it’s useless because it literally replaces ModuleScripts. I believe it’s deprecated and might cause errors.

Ok.

Server script in workspace

require(AssetId)

Module script the server script require

main = {}
main.folder= Instance.new("folder")

_G.Name = main

Local Script in player scripts

local Folder = require(_G.Name.folder.Module) --Module is inside of the folder

Anything else you need?

Since the server is requiring it (client can’t require by ID) then you are using it in the context of the server, therefore client can’t get _G.Name. This is exactly why I am discouraging _G – use modules that are shared between the server and client (which you want) in a folder in ReplicatedStorage and require that on the client.

1 Like

If you require a asset id it never appears in the explorer, so you cant access any modules or folders or anything

Do you need to require by ID though? I think this can be solved by just making it a module in replicated storage, so both server and client can see it, and your issue would be solved (and would also eliminate the need of _G)

Yes i do need to require it by id.