Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Where's the best spot to keep ModuleScripts?

Asked by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago

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?

3 answers

Log in to vote
5
Answered by
amanda 1059 Moderation Voter
6 years ago

Here are your three primary containers for ModuleScripts:

  1. ServerScriptService
    • Place module scripts here whenever they are only used by the Server.
  2. StarterPlayerScripts
    • Place module scripts here whenever they are only used by the Client.
  3. 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:

  • Is the ModuleScripts being required or does it have the potential to be required by more than one script?

    • Consider the option of placing it within a 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?

    • Make it a direct child of the script which is requiring it. If one object has multiple 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.

2
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! :) chomboghai 2044 — 6y
1
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. amanda 1059 — 6y
0
But how do you call the require statement? I cannot get require to recognize anything except workspace. probloximus 0 — 5y
0
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. amanda 1059 — 5y
View all comments (2 more)
0
For all others, quick note that I changed my Discord tag to amanda#0001. amanda 1059 — 5y
0
This is very helpful, I highy recommend this post to anybody learning how to use ModuleScripts. Thanks, Amanda! RealMathGeek 4 — 5y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

Log in to vote
0
Answered by
oreoollie 649 Moderation Voter
6 years ago

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!

0
Sorry this is my first time using this forum - how do I mark it as correct? chomboghai 2044 — 6y
0
Just press that little check mark left of my answer. oreoollie 649 — 6y
0
Ah, thanks. There seemed to have been a glitch, I had to log out and log back in. chomboghai 2044 — 6y
0
No problem. oreoollie 649 — 6y
0
Oh, and welcome to Scripting Helpers! oreoollie 649 — 6y

Answer this question