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

How should I be using module scripts?

I still don't understand what a module script is, how I use it, when I should use it, and the advantages of it. Can someone clarify?

1 answer

✓ Accepted answer

A Module Script is a script in which the functions of the script can be accessed from another script using the require method. Let's say that I have a Module Script in the Workspace and inside the Module Script it has a function called "testScript()". The module script will look like this:

local module = {}
	function module.testScript()
		print("It works!")
	end
return module

...and the code in another script would be:

local module = require(game.Workspace.ModuleScript)
module.testScript()

...which would run testScript() and output "It works!" because testScript() prints that out.

To use a module script, you must first create a ModuleScript in the Basic Objects menu (right clicking somewhere and navigating to "Insert Object". This will insert the base code for the ModuleScript, which is the "local module = {}" and the "return module" bit at the start and end of the script). Then you can start adding functions by typing in a function that uses the name of the Module Script (so in this case "module" is the name of the Module Script because at the start and end it says "local module= {}" and "return module"), adding in a dot (period, full stop, "."), and then the name of the function (in this case, I called it testScript()). The code should now look like this:

local module = {}
	function module.testScript()
		
	end
return module

...however the name of the function (module.testScript()) will vary depending on what you called the function. Note that the name of the function is "module.testScript()" because the name of the Module Script is called module. To change a name of the Module Script, change the word after "local" on the first line and the word after "return" on the last line, so it could be like this:

local helloThere = {}
	function helloThere.testScript()
		
	end
return helloThere

To then call the function from another script, you must first require the Module Script from the other script by using this line of code:

local myModuleScript = require(The Location of the Module Script. Example: game.Workspace.ModuleScript)

...and then you can finally call the function from the other script by using this line of code:

myModuleScript.testScript()

...where "testScript()" will be the name of your function. You should use Module Scripts when you want to publish something to the free models catalog but want to hide the script behind it, so what you can do is you can upload your Module Script seperately to the free models catalog and take it off sale, but the Module Script MUST be called "MainModule", and then when you require the Module Script, use the model ID instead. For Example:

local myModuleScript = require(123456789)

...where the number inside the require parenthesis will be your model ID. In short, Module Scripts allow you to call functions from another script and they are useful for creating an easy way to call functions or if you want to hide the code behind a free model.

This may have been confusing for you. If it was, then do not worry, I have to admit I didn't explain that one in full detail, but there's not much you can do with plain text. There are many YouTube tutorials on Module Scripts and this was just a small starter to what Module Scripts are.

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