Loader
The Loader module will require all children or descendant ModuleScripts. There are also some utility functions included, which assist with loading and starting modules in single-script environments.
For example, here is a loader that loads all ModuleScripts under a folder that end with the name Service, and then calls all of their OnStart methods:
local MyModules = ReplicatedStorage.MyModules
Loader.SpawnAll(
Loader.LoadDescendants(MyModules, Loader.MatchesName("Service$")),
"OnStart"
)
Types
PredicateFn
Predicate function type.
Functions
LoadChildren
Requires all children ModuleScripts.
If a predicate
function is provided, then the module will only
be loaded if the predicate returns true
for the the given
ModuleScript.
-- Load all ModuleScripts directly under MyModules:
Loader.LoadChildren(ReplicatedStorage.MyModules)
-- Load all ModuleScripts directly under MyModules if they have names ending in 'Service':
Loader.LoadChildren(ReplicatedStorage.MyModules, function(moduleScript)
return moduleScript.Name:match("Service$") ~= nil
end)
LoadDescendants
Requires all descendant ModuleScripts.
If a predicate
function is provided, then the module will only
be loaded if the predicate returns true
for the the given
ModuleScript.
-- Load all ModuleScripts under MyModules:
Loader.LoadDescendants(ReplicatedStorage.MyModules)
-- Load all ModuleScripts under MyModules if they have names ending in 'Service':
Loader.LoadDescendants(ReplicatedStorage.MyModules, function(moduleScript)
return moduleScript.Name:match("Service$") ~= nil
end)
MatchesName
A commonly-used predicate in the LoadChildren
and LoadDescendants
functions is one to match names. Therefore, the MatchesName
utility
function provides a quick way to create such predicates.
Loader.LoadDescendants(ReplicatedStorage.MyModules, Loader.MatchesName("Service$"))
SpawnAll
Loader.
SpawnAll
(
loadedModules:
{
[
string
]
:
any
}
,
methodName:
string
) →
(
)
Utility function for spawning a specific method in all given modules.
If a module does not contain the specified method, it is simply
skipped. Methods are called with task.spawn
internally.
For example, if the modules are expected to have an OnStart()
method,
then SpawnAll()
could be used to start all of them directly after
they have been loaded:
local MyModules = ReplicatedStorage.MyModules
-- Load all modules under MyModules and then call their OnStart methods:
Loader.SpawnAll(Loader.LoadDescendants(MyModules), "OnStart")
-- Same as above, but only loads modules with names that end with Service:
Loader.SpawnAll(
Loader.LoadDescendants(MyModules, Loader.MatchesName("Service$")),
"OnStart"
)