Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Does roblox allows modules in module?

Does roblox allow modules to be able to require other modules? Like...

-- MainModule
local Module 2 = require(###########)
-- Module 2 
local Module 2 = require (###########)

^^^Does this work on roblox and is this allowed in scripts?

1 answer

✓ Accepted answer

Yes, ModuleScripts are allowed to require other ModuleScripts. In fact, with more complex games, it is good practice to have sub-dependencies (with a hierarchical structure) rather than a flat structure. So for example, your modules could be like this:

GameModule  
 -> GunModule  
   -> BulletManagerModule  
 -> MapModule  
   -> VotingModule  

One thing that is not allowed is for ModuleScript loading to cause a cycle. That is, a ModuleScript A can't require a ModuleScript B so that it ModuleScript A gets required again before it has returned a value. For example, this is not allowed:

local self_module = require(script)

return {
    self_module = self_module
}

This is because requiring this module would cause the module to get required again infinitely many times.

However, this is allowed:

return {
    get_self_module = function()
        return require(script)
    end
}

The latter is allowed, because the require() can only run after the module has fully loaded (and returned a value).

1 comment

0

This was somewhat helpful thx.

This discussion is preserved as it was. New answers, comments, and votes are closed.