When to use module scripts

I have a list of things to try to learn by the end of this year and I am deciding it is a good time to learn module scripts. Can someone explain to me when to use it and how it is different between regular scripts?

1 Like

Sure.

A module script is a container that runs only once and will always return one value. They are essential if you follow by the rule ‘‘Do Not Repeat Yourself’’. You can call module scripts using require().

You could use it, for example, for UI animations. Your module script could handle all the code of the animation, and your local script inside of your UI would simply call it, hence playing the animation. This would shorten your workload and your code since you wouldn’t have to rewrite the animation per button.

Some people only use a few module scripts in their games. Others will have dozens of them. It really depends on your style of coding and your preferences.

The best resource, which has a fantastic explanation about module scripts if I didn’t explain it clear enough for you can be found here : ModuleScript | Documentation - Roblox Creator Hub

Have fun learning module scripts. They can be very useful to some people!

6 Likes

Will module scripts function the same if you put it in serverstorage or replicatedstorage?

ModuleScripts that are in ReplicatedStorage can be seen by both client and server. Putting them in ServerStorage only allows them to be seen by the server. If you have a sensitive chunk of code that you don’t want to be seen by random clients (for example, using an external web API with an API key), then you would be better off putting this in ServerStorage. Everything else can go in ReplicatedStorage for the most part.

3 Likes

So its better to put a module script in server storage then replicatedstorage because users can view the code?

Only if the code in that module is used strictly between scripts on the server and probably shouldn’t be seen by exploiters.

2 Likes

If you are fairly worried about exploiters seeing the code of a module for instance, if you need to use the module once in one script example a code system where you have codes in a module, I suggest you put the codes in the script instead.