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: (...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
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() → booleanDetects 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