Are there any ways to have modules globally without having to call require to path each time?

I’m wondering if there are any other ways I can have access to modules everywhere, I’ve seen things like https://create.roblox.com/store/asset/10238358164/RequirePlus but it’s clearly offsale and Import doesn’t make any sense to me.

What I thought of doing is using _G. Global variables to get access to each module everywhere or have a specific module cache everything in a table, which idea would be more suitable and are there any other ways to go about this?

1 Like

You could technically use _G to store your modules but that usually turns into a headache pretty fast since anything can overwrite it and it becomes hard to track where something is coming from.

A more reliable approach is to make a single loader module that requires and caches everything you need, then just require that loader wherever you need access. That way you only have one point of maintenance and you avoid the risk of polluting the global environment!!

It also keeps things clearer for anyone else reading your code since they can follow the loader and see exactly what is available. Import feels strange at first but the idea behind it is similar, centralizing your requires so you are not scattering paths everywhere…


If you want global style access without the downsides of true globals the loader pattern is usually the way to go! Hope this helps!!!

1 Like

Are there any examples? I made something of my own but I don’t know if it’s good enough, basically I loop through the Folders of the modules using GetDescedants() and then have a function which return the module that I need.

That approach of looping through modules with GetDescendants() is actually pretty common and works nicely if done right.


First up, GetDescendants() is an instance method that gives you every child at any depth under a parent—works like scanning the entire tree in one go.

You can find the official docs for it here: look up “Instance:GetDescendants” in the Roblox Creator documentation Roblox Creator Dashboard. Its also explained in community guides like the Roblox Wiki Roblox Wiki!

That pattern of building a loader that scans folders requires each ModuleScript stores them in a cache and then lets you fetch them by name is a really good way to handle things…

local Loader = {}
local cache = {}

function Loader.Require(folder)
    for _, item in ipairs(folder:GetDescendants()) do
        if item:IsA("ModuleScript") then
            local name = item.Name
            cache[name] = require(item)
        end
    end
end

function Loader.Get(name)
    return cache[name]
end

return Loader

This shows a similar structure from a DevForum thread where someone defined a loader that also handled “__load” and “__init” hooks and even assigned a global function _G.Get to fetch modules by name Developer Forum | Roblox!!


Another approach is from Sleitnicks RbxUtil.Loader… it provides utilities like LoadDescendants() to require all module scripts beneath a given folder, optionally filtering by name, and even SpawnAll() to automatically call a method like OnStart on each module. It looks something like this:

local modules = Loader.LoadDescendants(ReplicatedStorage.MyModules, Loader.MatchesName("Service$"))
Loader.SpawnAll(modules, "OnStart")

This scanning, requiring and initializing makes managing lots of modules much smoother sleitnick.github.io.


So yeah, your method of scanning once, caching modules and grabbing them via a lookup function is solid and very often used. If you want to extend it adding optional lifecycle hooks like __load or OnStart can make setup even cleaner!

And hooking into GetDescendants is very efficient as long as the folder you scan doesnt change too dramatically during runtime!


Hope that helps and gives you some ideas on whatever your doing!!

1 Like

Ignore this; I accidentally pressed reply even though i wasnt finished typing…

Wouldn’t it be better to have an internal function that runs GetDescendants once?

Even better if you use CollectionService instead of GetDescendants and tag all the ModuleScripts with the relevant tag!

Much more performant since you’re only getting ModuleScripts. As an added bonus, if, for whatever reason, you don’t want to require a specific module, you can just remove the tag instead of adding a blacklist to your code!

2 Likes

This is actually a great Idea, I never really thought of it!

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