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

What's the use of module scripts?

Asked by 5 years ago

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

1 answer

Log in to vote
1
Answered by
Troxure 87
5 years ago

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!

0
You should also mention this a great method of global variables, this should be used over _G. User#19524 175 — 5y
Ad

Answer this question