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

What is a ModuleScript?

Asked by 10 years ago

I only discovered this today. What is ModuleScript and what can it do?

1 answer

Log in to vote
2
Answered by 10 years ago

A module script is a script that can be used by other scripts using the require() function. When the module script is require()d, whatever the module script returns at the end can be stored in the script that require()s it. An example:

--ModuleScript (for simplicity, named ModuleScript, in Workspace)

local stuff = {}

stuff.value = 5

return stuff
--Main Script
local stuff = require(game.Workspace.ModuleScript)

print(stuff.value) --> 5

Like bindable functions, they're useful for sharing functionality between scripts, like if you had a set of functions a bunch of scripts used, you could put them in a module script instead of copy-pasting the function to every script.

1
More accurately, ModuleScript will only run a single time on the Server and each Client. The table 'returned' after each subsequent 'require' is a reference to the same Table. (Because of this, you can require the ModuleScript, and then change the contents of the returned Table, and all other Scripts with a reference to that table will receive the change.) adark 5487 — 10y
Ad

Answer this question