Here are your three primary containers for ModuleScripts:
ServerScriptService
- Place module scripts here whenever they are only used by the
Server.
StarterPlayerScripts
- Place module scripts here whenever they are only used by the
Client.
ReplicatedStorage
- Place module scripts here whenever they are used by the
Server and the Client.
Note:
ModuleScripts are extensions of either Scripts or LocalScripts. Whichever type of script requires it, that is the environment it will run in.
Objects in ReplicatedStorage are cloned to both the Server and the Client.
To answer your question, you should place ModuleScripts in a container that gives them access to only the environment they need to run in.
Most of your Modules should be going in ServerScriptService or StarterPlayerScripts, as they are specific to the Server or Client environments, respectfully. However, utility libraries can be used by both, and should be placed in ReplicatedStorage so each have a copy.
The reason you don't "throw everything into ReplicatedStorage because that is easier", is because one of the biggest reasons you use ModuleScripts, is for organization. Also, you risk compromising Server code by sharing it with the Client.
Past the initial containers, you may be asking:
- What about
Folders? Do I make a Folder for all my ModuleScripts?
- Nested
Folders?
- Should I just place the
ModuleScripts under the Script or LocalScript that uses it the most?
This all depends on the scale of your project, how many scripts are requiring your ModuleScripts, and what else is located in your selected storage container.
In ReplicatedStorage, if you are using it, good chance you have more than ModuleScripts within it. You should make a Folder labeled "UtilityModules" or something similar, and from there you can have as many nested categorical Folders within to further organize if you are so inclined.
In ServerScriptService or StarterPlayerScripts:
Finally, what about embedded ModuleScripts? Most likely you have done this or thought about it. A chain of ModuleScripts which require each other and are all nested within the children of each other.
This is something you should do if you are experienced with ModuleScripts and have a specific hierarchy structure that works for you.
You can place the ModuleScripts wherever you want, however what I've posted in this thread is my recommendation for anyone wanting to make the most out of using ModuleScripts.
6 comments
Fantastic explanation, I really appreciate this! I've just now finished organizing my modules before you posted this, and I'm glad I did it the way I did, because I did in fact end up making folders and nesting modules. Either way, really appreciate the help! :)
No problem. I use modules on a daily so this answer was my pleasure to write. If you have any questions about module usage or want additonal tips and tricks, my username on ROBLOX is amanda, and my Discord tag is amanda#9999.
But how do you call the require statement? I cannot get require to recognize anything except workspace.
probloximus, You should look up a YouTube tutorial on require if you've never used it before, otherwise you should open up a seperate question so that more people can see it. To give a full reply with cases and examples, I would need more space and formatting than this comment allows.
For all others, quick note that I changed my Discord tag to amanda#0001.
This is very helpful, I highy recommend this post to anybody learning how to use ModuleScripts. Thanks, Amanda!