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

What is a ModuleScript and when would I use one? [closed]

Asked by 9 years ago

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.

Locked by FearMeIAmLag, Muoshuu, and M39a9am3R

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
2
Answered by
Muoshuu 580 Moderation Voter
9 years ago

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

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) BlueTaslem 18071 — 9y
Ad