baydenator8/loader
Module loader for Roblox
Loader
A module loader for Roblox. It collects ModuleScripts from folders, tags or direct references, then runs them in three phases: load (require), init (:Init()) and start (:Start()). It also logs timing and warns about modules that yield too long. A client loader can wait for the server loader to finish before it starts.
Installation
With pesde:
pesde add baydenator8/loader
pesde install
Then require the package from wherever pesde installs it in your place (usually roblox_packages).
Quick start
Server (Script in ServerScriptService):
local Loader = require(path.to.Loader)
local server = Loader.new({
Loader_Tag = "LOAD_SERVER",
})
server:RegisterAsServer()
server:Start(script.Parent:WaitForChild("Services"))
Client (LocalScript in StarterPlayerScripts):
local Loader = require(path.to.Loader)
local client = Loader.new({
Loader_Tag = "LOAD_CLIENT",
})
client:WaitThenStart(script.Parent:WaitForChild("Handlers"))
Ready-made versions of these scripts are in examples. They aren't part of the published package.
Writing a module
A loaded module returns a table. Init and Start are optional, and both are called with : so they receive the module as self:
local MyService = {}
function MyService:Init()
-- Set up state. Runs after every module has been required.
end
function MyService:Start()
-- Begin work. Runs after every module has finished Init.
end
return MyService
Lifecycle
- Load: each queued module is
required in the order it was added. - Init:
:Init()is called on every loaded module that defines it. - Start:
:Start()is called on every module whoseInitsucceeded (or that has noInit).
Each step runs in its own thread with xpcall, so an error in one module is printed with a traceback and doesn't stop the others. A module that errors in Init is not started.
The loader waits for each module to finish before it moves to the next. If a module is still yielding after Yield_Threshold seconds, it warns, then warns again every 5 seconds until the module finishes.
Init and Start are called in the same order the modules were loaded.
When a server-registered loader finishes, it sets Workspace's Loader_ServerReady attribute to "Loaded". That attribute is what WaitThenStart waits for.
A loader can only be started once. After it finishes it clears its internal state.
API
Loader.new(settings: LoaderSettings): Loader
Creates a loader. settings must be a table, even an empty one. Missing keys are filled from the defaults, and unknown keys are dropped.
loader:RegisterAsServer()
Marks this loader as the server loader, so clients can use WaitThenStart. Server only, and only one loader can be registered.
loader:Add(object: Loadable): { ModuleScript }
Queues a ModuleScript, every ModuleScript in a Folder (up to Search_Depth), or an array of either. Returns the current load queue.
loader:Remove(object: Loadable): { ModuleScript }
Removes a ModuleScript, or a Folder's modules, from the load queue. Returns the current load queue.
loader:Start(...: Loadable)
Adds any objects passed in, plus any instances tagged with Loader_Tag, then runs the lifecycle at once.
loader:WaitThenStart(...: Loadable)
Same as Start, but waits until the server loader has finished first. It can't be called on the loader registered as the server.
Types
type Loadable = ModuleScript | Folder | { ModuleScript | Folder }
Settings
| Key | Type | Default | Description |
|---|---|---|---|
Search_Depth | number? | 1 | How deep to search a Folder for modules. 1 means direct children only. Set to math.huge to search all descendants. |
Yield_Threshold | number? | 5 | Seconds a module may yield before the loader warns about it. |
Verbose_Loading | boolean? | true | Print progress and timing for each phase. |
Loading_Prefix | string? | "[LOADER]" | Prefix for verbose log headings. |
Loader_Tag | string? | "" | A CollectionService tag. ModuleScripts and Folders with this tag are added on start. An empty string turns tagged loading off. |
Module attributes
Set these attributes on a ModuleScript to change how the loader treats it:
| Attribute | Effect |
|---|---|
IgnoreLoader | The module is never loaded. |
ClientOnly | The module is skipped on the server. |
ServerOnly | The module is skipped on the client. |
RelocateToServerScriptService | Server only. After the module is required, it is moved to ServerScriptService.RELOCATED_MODULES and a stub (ServerOnly) takes its place. Requiring the stub returns the real module. This keeps server code out of replicated containers while your folder layout stays the same. |
ClientOnly and ServerOnly are mainly useful for shared folders. Separate tags (Loader_Tag) per run context are usually the cleaner option.
Tagged modules and duplicates
If a module is both added with :Add/:Start and tagged with Loader_Tag, it can be loaded twice. This happens when the two references come from different places, such as a module in StarterPlayerScripts and its copy in PlayerScripts. The loader warns when it finds a tagged module whose name matches one already queued.
License
MIT