Skip to main content

ModuleLoader

ModuleScript loader that bypasses Roblox's require cache.

Roblox caches what a module returns the first time it's required, so a change to the module only takes effect after the place reloads. A loader ignores that cache: require a module, change it, clear the loader, and require it again to run the new code.

local ModuleLoader = require(ReplicatedStorage.Packages.ModuleLoader)

local loader = ModuleLoader.createModuleLoader()
local result = loader.require(module)

loader.loadedModuleChanged:Connect(function()
	loader.clear()
	result = loader.require(module)
end)

Types​

ModuleLoadError​

interface ModuleLoadError {
err: unknown--

The value the module raised, exactly as require raised it.

traceback: string--

The stack where the module raised it, one frame per line.

}

An error a module raised while it loaded, and the stack where it raised it.

ModuleLoader​

interface ModuleLoader {
require: (moduleScript: ModuleScript) → ...unknown--

Requires a module, and every module it requires, without Roblox's require cache. Requiring it again returns the same result until clear is called. A module's error is raised exactly as the module raised it, without a traceback.

cache: (
moduleScript: ModuleScript,
...unknown
) → ()--

Sets the result require returns for a module before it loads, so loaded modules share a copy you already have, such as a UI library.

clear: () → ()--

Empties the loader's cache, so the next require runs each module again.

getLastError: () → ModuleLoadError?--

Returns the error from the most recent failed require and the stack where the module raised it, or nil after a require that succeeds and after clear.

loadedModuleChanged: RBXScriptSignal--

Fires when a module required through this loader has its ancestry or Source changed.

}

A loader made by createModuleLoader.

Functions​

createModuleLoader​

ModuleLoader.createModuleLoader() → ModuleLoader

Creates a loader with its own cache.

A loader keeps what each module it requires returns, so requiring a module twice through one loader runs it once. Two loaders never share results.

local loader = ModuleLoader.createModuleLoader()
local result = loader.require(script.Parent.ModuleScript)

isSandboxed​

ModuleLoader.isSandboxed() → boolean

Detects whether the code calling this function is running in a ModuleLoader sandbox.

When a module is evaluated through a loader, it receives a proxy global environment (_G). Code running in that environment can call this function to detect if it is sandboxed. This is useful for modules that need to behave differently when loaded through a loader versus when loaded with native require().

Natively-required ModuleLoader always returns false, since it sees the real global environment without the sandbox marker. When called from code evaluated through a loader, this function returns true.

local ModuleLoader = require(ReplicatedStorage.Packages.ModuleLoader)

if ModuleLoader.isSandboxed() then
	print("Running in a sandbox!")
else
	print("Running natively")
end
Show raw api
{
    "functions": [
        {
            "name": "createModuleLoader",
            "desc": "Creates a loader with its own cache.\n\nA loader keeps what each module it requires returns, so requiring a module\ntwice through one loader runs it once. Two loaders never share results.\n\n```lua\nlocal loader = ModuleLoader.createModuleLoader()\nlocal result = loader.require(script.Parent.ModuleScript)\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ModuleLoader"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 23,
                "path": "src/init.luau"
            }
        },
        {
            "name": "isSandboxed",
            "desc": "Detects whether the code calling this function is running in a ModuleLoader sandbox.\n\nWhen a module is evaluated through a loader, it receives a proxy global\nenvironment (`_G`). Code running in that environment can call this function\nto detect if it is sandboxed. This is useful for modules that need to behave\ndifferently when loaded through a loader versus when loaded with native `require()`.\n\nNatively-required ModuleLoader always returns false, since it sees the real\nglobal environment without the sandbox marker. When called from code evaluated\nthrough a loader, this function returns true.\n\n```lua\nlocal ModuleLoader = require(ReplicatedStorage.Packages.ModuleLoader)\n\nif ModuleLoader.isSandboxed() then\n\tprint(\"Running in a sandbox!\")\nelse\n\tprint(\"Running natively\")\nend\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 27,
                "path": "src/isSandboxed.luau"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "ModuleLoadError",
            "desc": "An error a module raised while it loaded, and the stack where it raised it.",
            "fields": [
                {
                    "name": "err",
                    "lua_type": "unknown",
                    "desc": "The value the module raised, exactly as `require` raised it."
                },
                {
                    "name": "traceback",
                    "lua_type": "string",
                    "desc": "The stack where the module raised it, one frame per line."
                }
            ],
            "source": {
                "line": 26,
                "path": "src/types.luau"
            }
        },
        {
            "name": "ModuleLoader",
            "desc": "A loader made by `createModuleLoader`.",
            "fields": [
                {
                    "name": "require",
                    "lua_type": "(moduleScript: ModuleScript) -> ...unknown",
                    "desc": "Requires a module, and every module it requires, without Roblox's require cache. Requiring it again returns the same result until `clear` is called. A module's error is raised exactly as the module raised it, without a traceback."
                },
                {
                    "name": "cache",
                    "lua_type": "(moduleScript: ModuleScript, ...unknown) -> ()",
                    "desc": "Sets the result `require` returns for a module before it loads, so loaded modules share a copy you already have, such as a UI library."
                },
                {
                    "name": "clear",
                    "lua_type": "() -> ()",
                    "desc": "Empties the loader's cache, so the next `require` runs each module again."
                },
                {
                    "name": "getLastError",
                    "lua_type": "() -> ModuleLoadError?",
                    "desc": "Returns the error from the most recent failed `require` and the stack where the module raised it, or nil after a `require` that succeeds and after `clear`."
                },
                {
                    "name": "loadedModuleChanged",
                    "lua_type": "RBXScriptSignal",
                    "desc": "Fires when a module required through this loader has its ancestry or `Source` changed."
                }
            ],
            "source": {
                "line": 42,
                "path": "src/types.luau"
            }
        }
    ],
    "name": "ModuleLoader",
    "desc": "ModuleScript loader that bypasses Roblox's require cache.\n\nRoblox caches what a module returns the first time it's required, so a\nchange to the module only takes effect after the place reloads. A loader\nignores that cache: require a module, change it, clear the loader, and\nrequire it again to run the new code.\n\n```lua\nlocal ModuleLoader = require(ReplicatedStorage.Packages.ModuleLoader)\n\nlocal loader = ModuleLoader.createModuleLoader()\nlocal result = loader.require(module)\n\nloader.loadedModuleChanged:Connect(function()\n\tloader.clear()\n\tresult = loader.require(module)\nend)\n```",
    "source": {
        "line": 267,
        "path": "src/createModuleLoader.luau"
    }
}