I want to find out what Module Scripts are used for.
Basically, the code inside of a ModuleScript only ever is interpreted once: when it is first require
d. Once interpreted, you will have to return
a table of access functions to manipulate or use the content of the ModuleScript.
Any subsequent require
s will return a table identical to the table returned by the first require
, but will not cause the code to be interpreted again - any tables that are declared as empty will only be declared once, even if the ModuleScript is required
multiple times.
The most typical use of ModuleScripts is for creating functions you want to use in multiple Scripts. Using a ModuleScript for this solves two potential problems: having Global access to the function (through the _G or shared tables) when you don't want certain scripts to have access, and having to maintain multiple, identical copies of the function because you copied it to those multiple scripts in order to avoid the first problem.
For a more specific example, if you have some AI creatures for an RPG, they might all require a Pathfinding module, for, well, pathfinding. Any that don't, don't have to require
it, and the code for this pathfinding will all be in only one place: the ModuleScript itself. Now, a different set of AI creatures may need to be able to use specific types of weapons, or interact with the world in ways such as opening doors. Creating Modules for each of these functions, or even creating a generic 'NPC AI' Module will greatly reduce the amount of code you have to maintain, as well as reducing lag, as, again, the code only runs from one ModuleScript, even if you have hundreds of Scripts that require
and use it.
This is from the Roblox WIKI, I did not type this out, but I hope it is useful;
A ModuleScript is a Script-like object that contains code for a module that can be loaded by calling the require global function with the ModuleScript as an argument. Its code will only be executed once per peer (server/client). This makes it useful for creating libraries of code that are shared between scripts on the same peer.
Hope this helped!