Require | A lightweight, extended require module

Version 1.00 (7 February 2024)


Introduction

Hello, I just released a module that extends the functionality of Roblox’s default require function. Notable features include, but not limited to:

  • Able to use the default require function as normal.
  • Allows you to (un)register modules, even automatically with CollectionService.
  • Require registered modules with require("string")
  • Return multiple modules by passing multiple arguments.
  • Extra QOL functions.

Setup and Usage

Grab the model below and insert it in your game (I recommend ReplicatedStorage). The model contains the require ModuleScript and two Scripts inside it named init.server and init.client with the RunContext set to Server and Client respectively. These scripts just requires the parent module require with require(script.Parent) to allow it to initialize automatically.

To use it, you can use the following methods.

Method 1: Global Variable

The require module automatically sets the global variable _G.require to itself and sets an attribute named module to true in DataModel (commonly known as the global variable game) when it’s ready. You can use the following code to wait and grab the require module.

if game:GetAttribute("require") == nil then
	game:GetAttributeChangedSignal("require"):Wait()
end

local require = _G.require

Method 2: Standard Require

If you would like to use the standard require approach to grab the module, you can do that as well.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local require = require(ReplicatedStorage.require) -- WaitForChild if you are using a LocalScript

Changing Auto Register Settings

By default, the require module will auto register any modules using CollectionService with the require tag. If you would like to set a custom register name, you can add a string attribute named require with the value you want to register it as. If the require attribute is missing, it will use ModuleScript.Name instead.

If you would like to change these settings, open the require module and edit these lines conveniently located at the very top of the script:

-- boolean, whether to auto register ModuleScripts with the tag AUTO_REGISTER_TAG
local AUTO_REGISTER = true
-- boolean, whether to log any successful or failed messages
local AUTO_REGISTER_LOG = true
-- string, the tag used to grab ModuleScripts
local AUTO_REGISTER_TAG = "require"
-- string, the attribute used to set a custom register name
local AUTO_REGISTER_ATTRIBUTE = AUTO_REGISTER_TAG
-- number, the max amount of seconds require() will yield for if a module does not exist
local MAX_YIELD_TIME = 5

Model Link and Examples

You can grab the latest module anytime here:

Here is an example of the full functionality of this module:

-- Override the global variable require
if game:GetAttribute("require") == nil then
	game:GetAttributeChangedSignal("require"):Wait()
end

local require = _G.require

-- Require module(s)
local ExistingModuleA, RegisteredModuleB, IDModuleC = require(script.ExistingModuleA, "RegisteredModuleB", 0000000)

-- Register module(s)
local MyCustomModuleA = script.MyCustomModuleA
local MyCustomModuleB = require(script.MyCustomModuleB)

require:Register(MyCustomModuleA, "ModuleA")
require:Register(MyCustomModuleB, "ModuleB")
require:Register(0000000, "ModuleC")

-- Unregister module(s)
require:Unregister("ModuleA")
require:UnregisterAll()

-- Check if module name is registered
local someModuleIsRegistered = require:IsRegistered("SomeModule")

-- Get registered modules
local registeredModules = require:GetRegisteredModules()

for moduleName, module in pairs(registeredModules) do
	print(moduleName, module)
end

-- Get registered module names
local registeredModuleNames = require:GetRegisteredModuleNames()

print(string.format("Total modules registered: %d", #registeredModuleNames))
2 Likes