Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What's the use of module scripts?

What can I use module for and when should I be using it?

1 answer

✓ Accepted answer

You can use modules for many things. You can require them and have them return data. When you use a module script, you are required to return something at the end of the script, normally a table, which you will see by default if you add one into your game on studio. You can return functions which can be used through scripts, or local scripts along as the module is in the game ready, since locscripts can not require from an asset. Here are examples of a module's uses below...

In the module

local Table={};
Table.Ping=function()
print("pong!");
end
return Table; --return all of the data stored in the table so it can be accessed by other scripts by simply requiring this module.

In a script

local Module=require(workspace.Module)--lets say the module is in workspace.. require is used to access the module, or in other words 'load it'.
Module.Ping(); --Calling that function we made earlier in the module.

Output: ping!

Modules are used in many more ways then just returning a table with functions, or returning in general..

Remember that modules must be required in order to run the code within it.

I hope I helped you understand better!

1 comment

0

You should also mention this a great method of global variables, this should be used over _G.

This discussion is preserved as it was. New answers, comments, and votes are closed.