Want a special badge and 30 bux? Make some PolyWorld! Content and post it in the Creations forum!
Dark mode is incomplete! Please report any issues to the Discord.

ModuleScript

Scripts LocalScripts

ModuleScripts hold shared Lua you can require.
Use them for helpers, configs, and code reused by many Scripts or LocalScripts.

Inherits Instance.


Overview

Properties

Also inherits Instance properties.

Methods

Inherits Instance methods.


API

Properties

Source

Source : string
The Lua source of the ModuleScript.
In Studio this is the script editor contents.


require

local Util = require(script.Parent.Util)

require(moduleScript) runs the ModuleScript once and returns its return value.
Later requires of the same ModuleScript reuse the cached result.

The ModuleScript should return a table or function:

-- ModuleScript named Util
local Util = {}

function Util.clamp(n, lo, hi)
    if n < lo then return lo end
    if n > hi then return hi end
    return n
end

return Util

Create one with Instance.new("ModuleScript"), or place it in Studio under a Script / folder in the place.

Server Scripts can require ModuleScripts available on the server.
LocalScripts can require ModuleScripts available on the client.
A ModuleScript is not a running Script by itself.