Still have questions? Join our Discord server and get real time help.
Log in to vote

Does roblox allows modules in module?

Asked by 2 months ago

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

Log in to vote
Answered by
gskw 1026 Moderation Voter
2 months ago
Edited 2 months ago

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:

 -> 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)

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

This was somewhat helpful thx. THUNDER_WOW 149 — 2mo

Answer this question