_G for modulescript paths?

Hello average scripters/programmers/coders. So currently there is one thing that is a pain in the back to me to work with.

Paths. I have modules in replicated storage, server storage, and etc. I wanna require them. I will not afford to write local replicatedStorage, local modules, local module1, local module2 etc.
anymore. Im very sure there is something that will fix this pain in the back :tm:

Why not make a module that finds for descendants and return it?
Optimization and types. I lose them both.
Use roblox-TS
I wish I did. But its too late to port over to roblox-ts now.

So then I thought of _G. Every programmer says bla bla _G bad, but I wanna hear people who did this from experience. Not blindly believe popular opinions.

I would also like not “disgraced” alternatives if it was bad from experience. Thanks!

3 Likes

Being too lazy to require and getting the path to your module script sounds like something not many people struggle with. Also Modules are overall better in terms of organization, code readability, exploiting etc. I would still refrain from using _G and just stick with module scripts for this kind of stuff.

2 Likes

Its not laziness, its tediousness. What I meant is the path is too deep and tedious to write. So i would do like this:

require(_G.modules.module)

You could use ObjectValues if it really is such a long path. I still wouldn’t recommend this, you only have to require the module once per script. Your commonly used modules could be a intentionally short path.

image

local module = require(script.ModulePath.Value)

Interesting approach. However I would have to require a modulescript across multiple scripts. Now I can just copy paste the objectvalues and all, But I use Rojo.

I don’t see how Rojo inhibits this approach

what is _G? never used it before

It’s not advised to use global variables on a mass scale, it would be much easier if you made a framework that went through all possible locations of the said module and returned it if it has found a module with that name.

Because it opens up to a worse problem. I have to keep going to default.project.json and add objectvalues and reload rojo which at the end would be more tedious than the normal method.

bla bla _G bad for good reason. In normal Lua _G and shared are different, but in Roblox _G and shared function the same. _G and shared are still separate tables, but they do the same thing. The arguments I’m making here don’t necessarily apply to regular Lua.

The biggest problem with using _G is that invariably, people use it to hold static values. Things that always exist. _G is great for things that pop in and out of existence (i.e. as a list of parts correlating to a certain dynamic system in your game) but using _G for anything that doesn’t change is a mistake.

Let me share some history, because using _G for this purpose is a habitual holdover from before ModuleScripts. Until ModuleScripts (and BindableEvents) existed, scripts could not access the contents of other scripts. The only two ways to communicate between scripts was to change Value objects (i.e. StringValues etc) or use _G/shared That was never an issue in vanilla Lua. So Roblox developers used _G to communicate variables between scripts. This doesn’t work in vanilla Lua, but for Roblox developers it was essential.

The things that developers used _G for were always bad practice. Bad in a way that you’d get fired if you tried a similar system at a real company. But since ModuleScripts started existing and we have an alternative, we no longer need to resort to that bad practice.

Feel free to use _G if you need to hold a list of dynamic objects, but remember that even then, the vanilla Lua couterpart shared is seldom used in the real world. Dynamically populated mystery lists. Doesn’t sound so good when I word it like that, does it? You can’t tell from another file when the data will be populated, what the data will be, intellisense and typechecking is disabled, and when you write another file you need to consider the implications of storing something in a global table that may or may not be expecting a certain structure that you’re breaking. It’s difficult to read and impractical to maintain.

But no, other than what I’ve listed, there’s nothing wrong with it.

It’s the real world. Roblox developers are pampered with easy coding, they forget how lengthy things can be in the real world. But even if you want to go this route, you can make a directories ModuleScript instead of a directories _G. They’re functionally the same except one is easy to edit and maintain. I mean, you already need a script to set up the _G table, right? Just make it a ModuleScript and it’s already that much easier to edit in the future. And your scripts don’t need to wait for the data to exist. And you get typechecking and intellisense. It’s just so much better.

2 Likes

If its tedious you can probably create a macro for that.

I believe a vscode extension already does this, cant remember from the top of my head though. Roblox lsp perhaps? Ill get a screenshot if asked when I get back.

1 Like

Using _G for module script paths in Roblox is a matter of preference and coding style, but it does not necessarily make your code more efficient.
Accessing global variables or functions through _G can be slower than using local variables or module script paths, particularly in large codebases with many global variables or functions.

When it comes to module scripts, using local variables or module script paths is generally a safer and more efficient approach.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.