Studio / ROBLOX
ModuleScript require in Roblox Studio: returns, waiting and shared state
Trace a caller and a small settings module. Separate a missing return, an unexpected type, waiting inside a dependency and a recursive require error.
Find the caller before repairing the module #
A ModuleScript is reusable code loaded by a running caller. Adding it to Explorer is not proof that anything has required it. Begin with the exact message and the script that called require. Our fictional lantern workshop needs one table containing Label and HintSeconds. It does not issue rewards, save progress or load a numbered asset from another creator. Write down that agreement before changing the module: the caller expects a table with two known fields, not just any truthy value or a printed message in Output.
Distinguish three observations: the caller never reached require, require did not produce the expected result, or later code failed while using the result. Those are different investigation points. Keep the related general script-location guide nearby if the entry point does not run. This article adds the module's return agreement, reuse behavior and dependency checks. The standalone Luau checks described below really executed the return examples. They did not execute Roblox ModuleScript loading, replication, caching or Studio tests. The supplied Studio setup is a separate exercise to perform in an isolated practice project.
Build one module and one server caller #
Create a ModuleScript named LanternSettings directly under ReplicatedStorage. Paste the first code block into it. Then create a normal Script in ServerScriptService for the second block and use its server execution context. Names and parent relationships must match the example. The settings are public practice data, so this shared container is appropriate; it is not a location for passwords or server-only authority. The caller's bounded lookup checks that the object exists and really is a ModuleScript before trying to load it. Waiting for a name does not create the missing object.
Start a fresh Studio test and inspect Output in the server context. The caller marks startup, before require, returned and loaded after validating the table. The module has its own body marker. Read their order. Startup without before-require points to lookup; before-require without returned points to the module or dependencies; returned without loaded points to the return contract checks. Loaded means the example accepted the field types, not that a later gameplay operation succeeded. These are expected Studio observations, not results already measured here. Stop the test before applying a final source correction to the original objects.
print("MODULE: config body")
local config = {Label = "Lantern", HintSeconds = 3}
return config
local ReplicatedStorage = game:GetService("ReplicatedStorage")
print("CALLER: started")
local module = ReplicatedStorage:WaitForChild("LanternSettings", 5)
assert(module and module:IsA("ModuleScript"), "Expected LanternSettings ModuleScript")
print("CALLER: before require")
local config = require(module)
print("CALLER: returned", type(config))
assert(type(config) == "table", "LanternSettings must return a table")
assert(type(config.Label) == "string", "Expected Label string")
assert(type(config.HintSeconds) == "number", "Expected HintSeconds number")
print("CALLER: loaded", config.Label, config.HintSeconds)
local again = require(module)
print("Same reference in this context:", config == again)
Return exactly one non-nil value and the expected type #
The Creator Hub requires a ModuleScript to return one non-nil value. A table can contain several fields while remaining one returned value. The broken print-only example shows a different outcome: printing a message does not return the configuration to the caller. Likewise, returning two separate values is not a way to provide two settings to require. Put both settings in one table when that is your agreement. Check every possible path through the module body, including an early exit that reaches the end without returning what the caller expects.
Return count and type are separate questions. The pure Luau cases produced zero results for a body without return, one table for the working configuration, two results for a pair and one nil for return nil. One nil is not zero results, but neither satisfies this ModuleScript agreement. Returning a function or false produces one non-nil value; the caller must still expect that type. Our consumer explicitly requires a table. Replacing its check with if config then would confuse false with absence and still fail to verify the table's fields. The table below separates executed language observations from the documented module requirement.
print("MODULE: body reached")
Locate waiting and circular dependencies separately #
If the before-require marker appears but no result follows, inspect code that runs inside the module before its final return. A wait for an object, an event or another dependency can delay the caller. Mark the boundary before and after each such step in the isolated exercise. A pcall can report a thrown error; it is not a timeout for work that continues waiting. Do not add repeated require calls in a loop to force progress. First identify which operation has not finished and whether its expected input can actually arrive in that execution context.
Draw a tiny dependency list, for example LanternSettings uses RouteNames, while RouteNames should not need LanternSettings to initialize. The current Reuse code guide names Requested module was required recursively for circular loading. Record the actual error rather than promising that every cycle will silently hang forever. If two modules need the same constant, consider moving that constant to a third module that depends on neither of them. This is a design option to test, not an automatic repair for every project. Recheck the full dependency path after changing it, because renaming a single module does not remove a circular relationship.
Understand the reused reference and the execution side #
The official guide explains that requiring the same ModuleScript again in the same context returns the same reference rather than rerunning its body. The consumer includes a comparison for that observation. If the returned table is changed, later consumers in that context can see that changed table. This makes a settings table different from a fresh per-player inventory. Define whether the data should be shared configuration or independent mutable state. For independent state, a module can return functions that deliberately create new values; loading reusable code alone is not a promise of a new table every time.
Server and client loads have separate references according to the same guide. A shared source location does not make a server table mutation a network update for a client. Use the relevant communication system and server validation for actual gameplay. During diagnosis, record the caller's side and the exact module instance. A copied module, another place or another test context changes what you are comparing. For a clean source-edit check, stop and start a fresh test instead of assuming a repeated require reloads changed initialization. Engine reference comparisons and client/server observations remain tasks for the Studio exercise, not findings of our standalone language runner.
Verify the agreement and keep a useful handoff #
Use six checks: caller starts, correct module found, before-require reached, expected return received, expected fields read and intended reference behavior observed. Record failure at its actual stage. A nil-index error after loaded differs from a missing module before require. A recursive-module message differs from an ordinary wait. The language table shows six executed return cases; it does not certify an entire game. Keep the consumer and module as separate files with their intended locations so another developer can repeat the exercise without guessing which block belongs in a ModuleScript.
Before moving the pattern into a real system, test a missing module, a wrong returned type, a dependency failure and the relevant server/client contexts. Preserve the first useful error rather than replacing every failure with an empty table. An empty table may suppress one access error while leaving the required configuration absent. End the handoff with the selected module, caller path, last marker, exact message and next check. The useful result is an understood data contract and dependency path. No live game, account, purchase or player data was changed while preparing this guide.
| Example | Luau return count | Meaning for our caller |
|---|---|---|
| print(...) | 0 | No value: module contract unmet |
| return config | 1 | Table with required fields |
| return 3, true | 2 | Two values: invalid module contract |
| return nil | 1 | One nil: invalid for a module |
| return function() … end | 1 | Valid module type; this caller needs a table |
| return false | 1 | Valid module type; this caller needs a table |
Original sources
Roblox Creator Hub — ModuleScriptRoblox Creator Hub — Reuse code

