# ModuleScripts Copying code to multiple scripts is time consuming and hard to manage. A better way is to organize and reuse code is by using a <a class="api-link">ModuleScript</a>, a unique type of script that returns a single value, usually a table or function that is useful to multiple scripts. <div class="container"> <div class="row"> <div class="col-sm-12 col-md-12 col-lg-6 col-xl-6 bg-warning"> <figure class="figure"> <pre class="prettyprint linenums lang-lua"> -- Use require() to get the value returned by a ModuleScript local Car = require(script.Car) -- Use a ModuleScript variable print(Car.topSpeed) --> 120 -- Call a ModuleScript function Car.honk() --> Beep! </pre> <figcaption class="figure-caption text-center"><b>CarScript</b> (<code>Script</code> in <span class="text-nowrap"><code>ServerScriptService</code>)</span></figcaption> </figure> </div> <div class="col-sm-12 col-md-12 col-lg-6 col-xl-6 bg-danger"> <figure class="figure"> <pre class="prettyprint linenums lang-lua"> local Car = {} Car.topSpeed = 120 function Car.honk() print("Beep!") end -- This is returned by require() return Car </pre> <figcaption class="figure-caption text-center"><b>Car</b> (<code>ModuleScript</code> within <b>CarScript</b>)</figcaption> </figure> </div> </div> </div> ## Writing ModuleScripts <a class="api-link">ModuleScripts</a> are commonly placed in ServerScriptService when used by <span class="text-nowrap">server-side</span> Scripts and ReplicatedStorage when used by <span class="text-nowrap">client-side</span> <a class="api-link">LocalScripts</a>. However, <a class="api-link">ModuleScripts</a> can be used when parented to any object, as long as another script has access to it. When created, a <a class="api-link">ModuleScript</a> starts out with this code: <pre class="code-line-highlights" data-start="1" data-highlight=""> local module = {} return module </pre> The line `local module = {}` creates an empty table to store the module's contents. This table is then returned (`return module`) so that it's provided to other scripts which require this <a class="api-link">ModuleScript</a>. You should rename this table (and modify the return line) according to the module’s purpose, such as `PickupManager`. It's also a good idea to rename the ModuleScript to match. <pre class="code-line-highlights" data-start="1" data-highlight="1;3"> local PickupManager = {} return PickupManager </pre> ## Adding to ModuleScripts To add a function or variable to the table returned by the module, use the **dot notation** as illustrated below. ### Variables <pre class="code-line-highlights" data-start="1" data-highlight="4"> local PickupManager = {} -- Add a variable to the module table PickupManager.maxPickupDuration = 10 return PickupManager </pre> ### Functions <pre class="code-line-highlights" data-start="1" data-highlight="4;6"> local PickupManager = {} -- Add a function to the module table function PickupManager.getPickupBonus(rarity) end return PickupManager </pre> <div class="panel panel-info panel-collapse"> <div class="panel-heading"> Internal Module Functions/Variables </div> <div class="panel-body"> If certain variables or functions do **not** need to be exposed outside of the module, you can omit them from the returned table. Below, the `DEFAULT_MULTIPLIER` constant and `RARITY_MULTIPLIERS` table are only used by the module's getPickupBonus() function, so they don't need to be added to the PickupManager table. <pre class="code-line-highlights" data-start="1" data-highlight="3-9;13"> local PickupManager = {} local DEFAULT_MULTIPLIER = 1.25 local RARITY_MULTIPLIERS = { Common = 10, Uncommon = 20, Rare = 50, Legendary = 100 } -- Add a function to the module table function PickupManager.getPickupBonus(rarity) local bonus = RARITY_MULTIPLIERS[rarity] * DEFAULT_MULTIPLIER return bonus end return PickupManager </pre> </div> </div> ## Accessing Modules From Other Scripts A module script doesn't run its code by itself — another script needs to access it using the built-in require() function. This accepts the ModuleScript as its only argument. For example, if a ModuleScript is located within ReplicatedStorage, you can access it from another script as follows: <pre class="code-line-highlights" data-start="1" data-highlight="4-5"> local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Require module local PickupManager = require(ReplicatedStorage:WaitForChild("PickupManager")) </pre> <div class="alert alert-warning" role="alert"> The first time `require()` is called on a <a class="api-link">ModuleScript</a>, it will run **once** and return a single value (here, the top-level table `PickupManager`). Calling `require()` again will return the **exact same table**; the module itself will never run multiple times. </div> ## Calling Module Functions Once the module is loaded, you can call any of its functions with a similar dot notation format. <pre class="code-line-highlights" data-start="1" data-highlight="3-9;13"> local PickupManager = {} local defaultMultiplier = 1.25 local rarityMultipliers = { common = 10, uncommon = 20, rare = 50, legendary = 100 } -- Add a function to the module table function PickupManager.getPickupBonus(rarity) local bonus = rarityMultipliers[rarity] * defaultMultiplier return bonus end return PickupManager </pre> <pre class="code-line-highlights" data-start="1" data-highlight="4;7;8"> local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Require module local PickupManager = require(ReplicatedStorage:WaitForChild("PickupManager")) -- Call function within module local bonus = PickupManager.getPickupBonus("legendary") print(bonus) --> 125 </pre>