We need a new way to solve reused code

As a Roblox developer, the reusing of code is rather frustrating and time consuming in situations where you have lots of scripts and modules.


Recently I’ve found myself creating a module called ‘Library’ that holds everything from getting objects to creating and running animations.

However, to get this I am still required to go through the unsatisfying process of defining variables and going through the object tree until I can require the module.

As well as this, the module often does complex/destructive processes that shouldn’t be spammed across lots of scripts.

Finally, I am often using this ‘Library’ module to get other modules… which then require ‘Library’ again in their own code. This means I have to manually require() the modules that get returned, instead of requiring them through ‘Library’, as that would cause infinite recursion.

Passing values directly to modules is still impossible, so there are really no other options that aren’t ‘hacky’.

Related side question

https://devforum.roblox.com/t/question-about-the-debunked-topic-for-passing-arguments-to-modulescripts/71410

(_G would not work because it does not load before other scripts, and would require me to setup a repeat wait() process in every script. I also believe this topic has shown why we may not want to use it.)


Possible solution

Myself and others would much prefer it if we could create some sort of ModuleScript that we could put in a special location or give a special tag that would use globally in all of our scripts.

Not sure if this possible, though.

Another minor solution could be to allow us to toggle whether a ModuleScript returns a different instance every time it is required - or whether it loads at the start of the server and returns the exact same instance every time is required.


Solving this issue will help Roblox games to become more efficient in both performance and development. It will also encourage people to use better practices when scripting - as there are many users who still cling to old, slow and risky methods.

5 Likes

I believe require('tagname') style requires have been suggested, and would make for a great solution.

13 Likes

All you need is an environment manager. A library that does the requiring and loading work for you. Anything can be abstracted to a utility function.

Yup, but ReplicatedStorage is starting to make my eyes hurt. And like I said, I’ll still be requiring lots of instances of something that should just be required once.

1 Like

This is one of those rare occasions where a global is a reasonable approach.

local Backpack = shared.import'/game/ui/Backpack.client'

Waiting for the global isn’t an issue because everything is required after shared is loaded.
It’s set up so that the root path is ReplicatedStorage.src and '/server' is a symlink to ServerStorage.src.

It’d be interesting for us to support something similar natively. Any solution would have to be designed with an eye towards configurability since everyone structures their games differently.

Injecting variables into the global environment should not be considered a solution. Variables need to be explicitly defined for static analysis to work.

2 Likes