ModuleScript
ModuleScript : LuaSourceContainer
Overview
The ModuleScript class is a script type designed for storing reusable Lua code that can be shared across multiple scripts in a game. Unlike Script and LocalScript, ModuleScript does not execute automatically. Instead, its contents are accessed by calling the require() function, which returns the data or functions defined in the module. This makes it ideal for organizing code, implementing object-oriented programming (OOP), and managing shared data or utilities.
Description
The ModuleScript class provides a flexible way to encapsulate code and share it across different parts of a OVERDARE UGC. It is commonly used to create utility functions, data storage, or classes for OOP, allowing developers to maintain clean and modular codebases.
Key Features
Reusable Code:
Code written in a
ModuleScript
can be accessed from multiple scripts using therequire()
function. This eliminates redundancy and simplifies maintenance.The module runs only once per environment (server or client) when it is first required. Subsequent calls to
require()
return the same reference.
Data and Function Storage:
A
ModuleScript
typically returns a table containing functions, variables, or other data that can be accessed by the requiring script.Developers can store constants, configurations, or shared logic in a single place.
Environment-Specific Execution:
A
ModuleScript
executes in the same environment as the script that requires it. If required by a server-side script, it runs on the server; if required by a client-side script, it runs on the client.
Object-Oriented Programming (OOP):
Developers often use
ModuleScripts
to implement OOP by defining classes and methods with metatables.
Usage
The ModuleScript
is commonly used for:
Utility Functions:
Centralizing reusable functions (e.g., math utilities, string manipulation).
Shared Data:
Storing global data or settings that need to be accessed by multiple scripts.
Object-Oriented Programming:
Defining classes and creating objects with methods and properties.
Encapsulation:
Keeping code organized and modular for better readability and maintainability.