Skip to main content

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

type PredicateFn = (moduleModuleScript) → boolean

Predicate function type.

Functions

LoadChildren

Loader.LoadChildren(
parentInstance,
predicatePredicateFn?
) → {[string]any}

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

Loader.LoadDescendants(
parentInstance,
predicatePredicateFn?
) → {[string]any}

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

Loader.MatchesName(matchNamestring) → (moduleModuleScript) → boolean

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},
methodNamestring
) → ()

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"
)
Show raw api
{
    "functions": [
        {
            "name": "LoadChildren",
            "desc": "Requires all children ModuleScripts.\n\nIf a `predicate` function is provided, then the module will only\nbe loaded if the predicate returns `true` for the the given\nModuleScript.\n\n```lua\n-- Load all ModuleScripts directly under MyModules:\nLoader.LoadChildren(ReplicatedStorage.MyModules)\n\n-- Load all ModuleScripts directly under MyModules if they have names ending in 'Service':\nLoader.LoadChildren(ReplicatedStorage.MyModules, function(moduleScript)\n\treturn moduleScript.Name:match(\"Service$\") ~= nil\nend)\n```",
            "params": [
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "predicate",
                    "desc": "",
                    "lua_type": "PredicateFn?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ [string]: any }\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 44,
                "path": "modules/loader/init.lua"
            }
        },
        {
            "name": "LoadDescendants",
            "desc": "Requires all descendant ModuleScripts.\n\nIf a `predicate` function is provided, then the module will only\nbe loaded if the predicate returns `true` for the the given\nModuleScript.\n\n```lua\n-- Load all ModuleScripts under MyModules:\nLoader.LoadDescendants(ReplicatedStorage.MyModules)\n\n-- Load all ModuleScripts under MyModules if they have names ending in 'Service':\nLoader.LoadDescendants(ReplicatedStorage.MyModules, function(moduleScript)\n\treturn moduleScript.Name:match(\"Service$\") ~= nil\nend)",
            "params": [
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "predicate",
                    "desc": "",
                    "lua_type": "PredicateFn?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ [string]: any }\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 74,
                "path": "modules/loader/init.lua"
            }
        },
        {
            "name": "MatchesName",
            "desc": "A commonly-used predicate in the `LoadChildren` and `LoadDescendants`\nfunctions is one to match names. Therefore, the `MatchesName` utility\nfunction provides a quick way to create such predicates.\n\n```lua\nLoader.LoadDescendants(ReplicatedStorage.MyModules, Loader.MatchesName(\"Service$\"))\n```",
            "params": [
                {
                    "name": "matchName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "(module: ModuleScript) -> boolean\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 97,
                "path": "modules/loader/init.lua"
            }
        },
        {
            "name": "SpawnAll",
            "desc": "Utility function for spawning a specific method in all given modules.\nIf a module does not contain the specified method, it is simply\nskipped. Methods are called with `task.spawn` internally.\n\nFor example, if the modules are expected to have an `OnStart()` method,\nthen `SpawnAll()` could be used to start all of them directly after\nthey have been loaded:\n\n```lua\nlocal MyModules = ReplicatedStorage.MyModules\n\n-- Load all modules under MyModules and then call their OnStart methods:\nLoader.SpawnAll(Loader.LoadDescendants(MyModules), \"OnStart\")\n\n-- Same as above, but only loads modules with names that end with Service:\nLoader.SpawnAll(\n\tLoader.LoadDescendants(MyModules, Loader.MatchesName(\"Service$\")),\n\t\"OnStart\"\n)\n```",
            "params": [
                {
                    "name": "loadedModules",
                    "desc": "",
                    "lua_type": "{ [string]: any }"
                },
                {
                    "name": "methodName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 125,
                "path": "modules/loader/init.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "PredicateFn",
            "desc": "Predicate function type.",
            "lua_type": "(module: ModuleScript) -> boolean",
            "source": {
                "line": 25,
                "path": "modules/loader/init.lua"
            }
        }
    ],
    "name": "Loader",
    "desc": "The Loader module will require all children or descendant ModuleScripts. There are also\nsome utility functions included, which assist with loading and starting modules in\nsingle-script environments.\n\nFor example, here is a loader that loads all ModuleScripts under a folder that end with\nthe name Service, and then calls all of their OnStart methods:\n```lua\nlocal MyModules = ReplicatedStorage.MyModules\nLoader.SpawnAll(\n\tLoader.LoadDescendants(MyModules, Loader.MatchesName(\"Service$\")),\n\t\"OnStart\"\n)\n```",
    "source": {
        "line": 18,
        "path": "modules/loader/init.lua"
    }
}