# ModuleScripts
## Table of Contents
- [What are they?](https://hackmd.io/@Drastic/SJyDwML3R#What-are-they?)
- [What can they be used for?](https://hackmd.io/@Drastic/SJyDwML3R#What-can-they-be-used-for?)
- [Why are they useful?](https://hackmd.io/@Drastic/SJyDwML3R#Why-are-they-useful?)
- [How do i use them?](https://hackmd.io/@Drastic/SJyDwML3R#How-do-i-use-them?)
- [Setup](https://hackmd.io/@Drastic/SJyDwML3R#Setup)
- [Running ModuleScripts](https://hackmd.io/@Drastic/SJyDwML3R#Running-ModuleScripts)
- [Functions in ModuleScripts](https://hackmd.io/@Drastic/SJyDwML3R#Functions-in-ModuleScripts)
- [How they work](https://hackmd.io/@Drastic/SJyDwML3R#How-they-work)
- [What not to do](https://hackmd.io/@Drastic/SJyDwML3R#What-not-to-do)
## What are they?
ModuleScripts are one of the 3 script types. they are designed to be able to have other scripts be able to access their data that they return and have it stay the same across all scripts that are in the same RunContext. A RunContext is wether the script is running on the Client or the Server, ModuleScripts can run on both but their data will have separate versions for each.
## What can they be used for?
ModuleScripts can be used for a lot of things like; Metadata, Classes (Metatables), Storing functions you will use often, or just having a table full of values. they are arguably one of the best things roblox has added.
## Why are they useful?
ModuleScripts are useful because they prevent you from having to duplicate code a lot and can let you store data across multiple scripts.
## How do I use them?
### Setup
To setup a ModuleScript you need to add a module script to your game and open it up.
After opening it you should see
```lua
local module = {}
return module
```
ModuleScripts must alway return something, it could even return nil but it must have a return.
### Running ModuleScripts
ModuleScripts need a return because of how they are ran, to run a module you use `require()` and it returns whatever value is returned from your module.
**Example**
```lua
-- ModuleScript
local Module = {}
Module.TestValue = 10
return Module
-- Run ModuleScript
local MyModule = require(path.to.myModule)
print(MyModule)
-- {
-- TestValue = 10
-- }
```
## Functions in ModuleScripts
Adding functions in a ModuleScript is useful because you can call those functions from other scripts.
To make a function that another script can call you will do `function ModuleName.FunctionName(...)`
To make a function that other scripts cannot access you can just make a local function
```lua
local module = {}
-- Only accessable from inside the module.
local function LocalFunctionExample(...)
end
-- Other scripts can access this.
function module.ExampleFunction(...)
end
return module
```
## How they work
Once you require a module ALL of its data will be global untill its changed. this includes variables, any changed made to variables will carry over to all scripts that the module was required from.
```lua
local module = {}
local stringVariable = ""
function module.SetStringVariable(newString)
stringVariable = newString
end
function module.GetStringVariable()
return stringVariable
end
return module
--> Usage
local MyModule = require(path.to.myModule)
MyModule.SetStringVariable("100")
print(MyModule.GetStringVariable())
```
## What not to do
Module scripts can be "Recursively required". Recursive Requiring is when two ModuleScripts require each other before they are loaded which will cause them to error. 90% of the time when this becomes an issue it is due to a flaw with your script archetecture and you should rethink how your doing things.