Connections and repeatably requiring modules within modules

Hi,

I have what I believe should be a quick question about module script functionality.

I have a game with multiple inventory type items which each have a respective module. There are some other modules in my game (e.g. Cooldown, Vitality, Effects, etc.) which are expectedly going to rely heavily on these ‘core’ modules.

At the moment I am worried about the usage of repetitively requiring these ‘core’ modules into each item module, which are then all consolidated into a ‘control’ script.

Basically I am worried that there will be unnecessary replication of these repetitively used modules. At this time I am using bindable event and functions in scripts which have already required those mentioned modules.

Also I have placed all connections (RBLXScriptSignal) outside of the mentioned ‘core’ models which are repeatably required because i worry that multiple connections will be created by each require(), is this true?

Is this a valid concern?

Thank you in advance

If I understand your question correctly, you’re scared you’ll be repeatedly creating connections every time you require these ‘core’ modulescripts?

1 Like

Yes that is one concern, another is that requiring these scripts, possibly 100+ times across other smaller module scripts will become impractical for the server.

Thank you

I believe that code which only needs to be executed once should not be included in the returned module code that is required. Instead, consider using an Init method or function within the core module scripts, which is called only once. However, if you’re not encountering any problems, errors, or issues with repeatedly requiring the module, there may not be a need to change your project structure.

1 Like

I may have misunderstood your question, so please correct me if I’m mistaken. So I’m sorta just making assumptions of your project structure.

1 Like

Fantastic thank you! I was not aware of the module initialiser, thank you so much.

Not exactly a built-in method for modulescripts. Just a way I would approach it personally. So it would look something like:

-- assuming you have a main/loader script
local coreModule = require(blah.blah.epicmodulescript)
coreModule:Init()

1 Like

Great follow up post here

Requires are cached, so code in it should only run once, but I recommend you use an Init function to keep your code cleaner rather than it just being loosely in the default scope.

2 Likes

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