So I'm a big fan of modules, I use them practically anywhere I can, I like to keep everything neat and organized. However, I recently came up with an issue:
Originally, I kept all my scripts in ServerScriptService unless required to be in PlayerGui or a specific model. I have one script that I just realized shouldn't belong in ServerScriptService; my Enumerations module.
It is just a module that contains a bunch of global variables to keep things consistent throughout my game. However, in a local script I can't access ServerScriptService, and now I am curious if there is a better spot to keep all my modules - or just this Enums module.
Any tips?
Here are your three primary containers for ModuleScripts
:
ServerScriptService
Server
.StarterPlayerScripts
Client
.ReplicatedStorage
Server
and the Client
.Note:
ModuleScripts
are extensions of either Scripts
or LocalScripts
. Whichever type of script require
s 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:
Folders
? Do I make a Folder
for all my ModuleScripts
?Folders
?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
:
Is the ModuleScripts
being required or does it have the potential to be required by more than one script?
Folder
for organization, similar to the ReplicatedStorage
example. Otherwise, you will find yourself referencing crazy paths just to use a ModuleScripts
embedded inside another object.Is the ModuleScripts
being required by just one script?
ModuleScripts
embedded under it, but also other objects, you should consider making an embedded Folder
.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
.
It depends on what you need your modules to do- if your modules need to be accessed by the server as well as the client, they should be stored in ReplicatedStorage. Otherwise, they should be stored in ServerScriptService or ServerStorage for server-only access. Basically, for security sake, they shouldn't be stored in ReplicatedStorage if only the server should have access to them. For example, you don't want your very important datastore module in ReplicatedStorage because then the client(s) can use them.
I think it depends on what the module script will be used for. If it's used by one script and designed to keep the code separate and clean, they I would parent it to the script that uses it. If it's a script that will be used globally by the whole server, then I recommend keeping it in ServerScriptService. If it needs to be used by both the client and the server then you should place it in ReplicatedStorage because both the client and the server have access to it. If you want to keep everything as organized as possible, you could put a "Modules" folder inside of ServerScriptService and/or ReplicatedStorage.
Hope my answer helped! If it solved you problem please remember to mark it as correct!