Skip to main content
Version: 0.4.0.0

๐Ÿงฐ GetModule

GetModule is Shard's centralized module registry. It allows you to access any initialized service, controller, or utility module from anywhere in your game after it's been registered by the framework bootstrapper.

It supports:

  • Retrieving Shard-managed modules (e.g. controllers/services)
  • Fallback resolution from ReplicatedStorage or ServerStorage if not found
  • Optional debug logging for fallback visibility

๐Ÿ“ฆ When Should You Use GetModule?โ€‹

  • To access a service from another service (RoundService โ†’ DataService)
  • To access a controller from another controller (UIController โ†’ PromptController)
  • To share utility modules between environments (e.g. a type module or config table)
  • When doing manual dependency resolution across folders

๐Ÿงช Basic Usageโ€‹

local GetModule = require(ReplicatedStorage.Packages.GetModule)

local SpawnService = GetModule("SpawnService")
local RoundService = GetModule("RoundService")

SpawnService:SpawnPlayerAtRandomSpawn(player)
task.wait(5)
RoundService:StartNextRound()


๐Ÿงฌ How It Worksโ€‹

During startup, every module that is loaded through the bootstrapper is added to the registry:

GetModule:Add(required, moduleName)

Once registered, you can retrieve it with:

local MyService = GetModule("MyService")

If the module isn't found in the registry, GetModule will search:

  1. ReplicatedStorage
  2. ServerStorage (on the server)
  3. StarterPlayerScripts (on the client)

If a fallback is found and required successfully, it will also be cached for future use.


๐Ÿง  Tips & Best Practicesโ€‹

  • Always call GetModule() inside start() methods, never inside initialize(), to ensure all dependencies are already registered.
  • Avoid circular dependencies. If ServiceA uses ServiceB and vice versa, extract shared logic into a third module.
  • Use GetModule.Debug = true to enable warnings when fallback modules are required.

๐Ÿ” Example: Optional Fallbackโ€‹

-- SharedTypes may not exist, but if it does, this will find it
local SharedTypes = GetModule("SharedTypes")
if SharedTypes then
print(SharedTypes.ItemType)
end

๐Ÿ“ฆ APIโ€‹

MethodDescription
GetModule(name)Retrieve a module by name. If not found, it will search ReplicatedStorage and ServerStorage.
GetModule:Add(module, name)Register a module in the GetModule registry. This is done automatically during the bootstrap process.
GetModule:Remove(name)Remove a module from the registry. This is not commonly used.
GetModule.DebugEnable debug logging for fallback resolution. Useful for development. By default, this is tied directly to ReplicatedStorage.Configs.Framework, GET_MODULE_DEBUG
GetModule:Resolve(name)Resolve a module by name, returning the module or nil if not found. If found it is cached. This is an internal method.