Using shared or _G for Modules? [CLICK ME]

What are reasonable use cases for global tables? I was thinking of having a “module loader” type of server script that would essentially load all of my frequently used modules and store them in the shared table. What do you think about this?

I have just been introduced to the idea of the shared and _G global tables, and I’m not exactly sure how performant they are and how they are commonly used. If this is a reasonable idea, should I use shared or _G?

Any help is greatly appreciated. Thanks!

Using _G or shared is almost always a poor idea. Global state is unpredictable and makes things extremely difficult to debug. You likely want to internally manage dependency injection within your bootstrapper/module loader and modularize your code. Look at frameworks like Knit, Flamework, and others that have already solved these issues before, study their approaches, and decide how you want to structure your codebase.

Thank you for sharing this! I did some research on module loaders; however, many people told me that they are useless—the Roblox require is more than enough. What do you think about module loaders? If they are useful, I don’t think I am interested in reinventing the wheel. Do you have any open-source recommendations? I heard that Knit is outdated. I’m not too sure about Flamework, though.

Also, as for the resource you provided, what if I only use the global tables for modules that do not use OOP? Or what if I use it only for the path to the module? From my understanding, shared doesn’t have types (or is it possible to give types to shared values?), but perhaps there may be a workaround.

It is really up to your own preference. To be clear, a bootstrapper typically is a central script which implements a few comfort features for scripting using module scripts and can be as simple or complex as you like. The absolute most basic version of this would be a script that simply iterates over all the modules you want to load and requires them. This introduces a few issues however;

Imagine you have two modules, module A, and module B. Both modules have been required by your bootstrapper and are running, doing whatever they do. Consider that, module A must communicate with, call a method of, or otherwise interact with module B. A simple solution for this is to just require module B from module A and run the function, however when you do this, module A is now creating a new running instance of module B, throwing away it’s own internal state that it has been maintaining since the bootstrapper started it.

Instead, bootstrappers typically maintain one singular reference to each module, effectively making them “singletons,” then whenever module A wants something from module B, the bootstrapper steps in and hands module A it’s master reference to module B, where it can then call that method without losing module B’s internal state. You could simply write your modules around this limitation, and have them not keep any internal state, however at that point you may as well abandon using modules entirely and just use normal scripts (which is also a valid approach).

Many other problems exist that you will inevitably discover attempting to create a bootstrapper. There are many such ways to solve these issues and you will find that many solutions exist. These are typically referred to as ECS frameworks. Here are a few popular ones I know of:

fwiw, Flamework is a Typescript implementation of an ECS framework. Look into roblox-ts if you are interested, but roblox-ts is generally falling out of popularity as the new type solver is developed. It has a good community and a developed package ecosystem however is generally irrelevant for new projects.

I think the main important thing to keep at the center of your thoughts is to keep your project simple. Is it actually complex enough to require an entire framework, or can you get by simply by using scripts and events? Is that maintainable? Do you even care about maintainability? Frameworks are attractive to abstract away lots of issues, but sometimes they can bloat your project and make it harder to work within.

There is still not really a reason to use global tables here. Have your bootstrapper maintain a master reference to each of your modules, then create an interface between your modules and the bootstrapper that allows them to access the master references.

Both module loaders and globals are bad, you should never use any of them, module loaders for example is a straight up misuse of module scripts

Wow, thank you so much for this extensively in-depth reply! You’ve highlighted so many crucial aspects and provided numerous effective solutions and suggestions. I really couldn’t ask for more. I do have one question that may be as a result of my lacking intellect. You mentioned that I will inevitably encounter many problems, which I do not doubt at all, if I attempt to create my own bootstrapper, but you later stated that the most important thing to ask myself is whether I need a framework or not. I don’t believe I do, at least for now. Does this mean that I don’t need a bootstrapper either? Currently, the only modules that require other modules do not utilize OOP. They are more so utility functions or handle information on the entire run context level, rather than on each instance of the module. So, what do you think? I think I just need this last bit of clarification, and then we can wrap this topic up!

So would you instead recommend that I manually require other modules within a module?

If a module script does not return a value, you are already misusing the module script (unless it’s a MainModule, but that’s a different story).
Just use regular scripts instead without creating a nightmare and a super huge stack.

This topic arose when I was attempting to make abilities for different weapons. I pondered for some time on how I should do this before eventually settling on module scripts with multiple functions like OnHit and OnAbility. My problem, however, was that I would have to require my DamageHandler module in EACH of the ability modules—I have like more than 20 weapons. So my module is returning a table of functions (like most modules).

I think I misunderstood your original question. For this, no, you don’t need a framework, module loader, or anything else complex like that. Requiring that module doesn’t seem like a bad pattern to me; what is the issue with it? Any other method, like creating a base class, which in effect you’ve already done, would necessitate this anyway.

Alright, thanks a lot. I think I know how I will be approaching this now. I really appreciate both of your help. Thank you so much for your efforts, I will be closing this topic now.