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

What is a ModuleScript and when would I use one?

I have been reading up on ModuleScripts recently and I'm still trying to figure out exactly how they work. I read the Wiki ModuleScripts and Module Scripts Tutorial pages but I still don't fully understand how I would use one or when it would be appropriate to use one. I am inexperienced with ModuleScripts so maybe somebody else knows how to explain this to me.

1 answer

✓ Accepted answer

A ModuleScript is an object that gives the ability to create a reusable library of code. ModuleScripts are called using the function 'require()', and must always return something. Anything returned can be used within the script that called require() on it, and any number of scripts can do so. You'd likely use a ModuleScript to create a library usable by both Client and Server.

Here is an example of how to use one:

ModuleScript:

local library={
	foo=(function()
		print("bar")
	end)
}
return library

Script:

local Module=require(PathToModule)
print(Module.foo())

Output: bar

1 comment

0

To add on to this: If you have two scripts that need to do the same thing, use a ModuleScript to do that thing (instead of including the code twice)

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