Is there a way to get ModuleScript errors while loading them?

I use several ModuleScripts in my current project and occasionally I’ll get an error in the output that reads “The requested module experienced an error while loading”

It’s not consistent, and I’m really inclined to believe it has something to do with errors loading Roblox assets, as some days it’s worse than others.

I’ve debugged it in the past by adding print() statements everywhere, but I was wondering if there was an easier way to get the full error message?

Any help is greatly appreciated. Thanks in advance.

1 Like

Check the error message after the lines of ModuleScripts, where the “The requested module experienced an error while loading” reads. We’re not receiving enough information of what causes it.

No, that’s my exact problem. All is gives me is that error message and then a reference to the line that says require(moduleScript). There’s basically no information about the error and that’s what I’m looking to find.

If you fully wrapped your ModuleScript code in a pcall, and returned that then you could possibly get if it suceeded and the response. You may need to pack it into a table due to ModuleScripts only returning one value.

Here’s an example:

--// ModuleScript
return table.pack(pcall(function()
    error("Stinky error")
--// Script
local Data = require(ModuleScript)
print(Data[1]) --// Boolean, false
print(Data[2]) --// Response, Stinky error

Yeah it’s something that the Roblox error handler does. For example if I did something like

local function changeColour(part)
    part.BrickColor ="Really red")

changeColour(workspace.Part1) --let's pretend Part1 didn't exist, which
--will throw an error of course

If you click the error to know where it’s from, it doesn’t show you the line that called the function, it shows you the line that errored from the original function, in this case the 2nd line

    part.BrickColor ="Really red")

Instead of


Right, but if I understand you correctly that’s not going to give me any more information than I already have. Your code will basically just print out “Stinky error” if there was any error whatsoever in loading the ModuleScript, which I already know about. I want to know the specifics about the error caused.


I realize this too. If I click on the error message, it just brings me to the line that says require(moduleScript), not to the line in the ModuleScript that actually caused the error.

Since the beginning of this post I’ve figured out what the error was and patched it, but my original question still remains about how to get the error details.


Pretty sure that is because of what a module script actually is. You are essentially taking a clone of the module script when you do:

local constant = require(ModuleScript)

As such the module script isn’t causing an error, it’s the constant (a copy of the module script which we have defined) which has the error. As such I guess this is considered expected behaviour.

Okay so basically a ModuleScript is a set of code stored in a table that gets returned to a require call, right? That means that the variable the module script is stored in is just simply a table of functions and values, and the code not contained within a function is run on the original script’s current thread.

In other words, the original script’s thread ‘jumps’ over to the ModuleScript, and runs code until a value is returned, which ends up being a table of functions (usually). That table reference is then stored in the variable set by the require call.

This means that whatever error was caused by the ModuleScript while ‘loading’ (running ModuleScript code before a value is returned), is encountered by the original script’s thread. The problem is that the error doesn’t give me the specifics from where it encountered an error in the ModuleScript’s code, but rather gives me a generic message and a reference to where it started running ModuleScript code in the original main script.

1 Like

So what was the problem?

In case the ModuleScript’s code is made by yourself, then I suggest you spend some time to study “what could cause an error to occur, when this code is parsed and run the first time.” - Or even if it is not made by you, it is a good idea to study and understand “what is this code actually doing anyway?

If it is not a consistent error, then I would think that most times, its due to the modulescript’s code is attempting to use something that isn’t available yet. - Perhaps a Workspace object/part/… that hasn’t yet been created, and the modulescript’s code is not using proper WaitForChild(...) calls.

Or maybe if several people are modifying/coding using Team Create, perhaps it could be down to simple syntax errors in the code?

Using print() statements (as you so indicate you do), at the outer-most scope of the modulescript’s code, could help you narrow down, how far into the loading, parsing and execution of the code it has reached, before “an error occurs, that caused the require statement to fail.”

You don’t have to delete those print() statements, just “comment them out” afterwards, if you think you need to use them again later.


-- ModuleScript (that expect a certain object to already exist in Workspace)
local module = {}

module.searchFor = function(searchTxt)
  -- ...

local neededPart = game:GetService("Workspace"):FindFirstChild("Settings", true)
if neededPart.UseRegularExpression.Value == true then -- This statement will cause error, if `neededPart` is nil.
  module.regExSearchFor = function(regExSearchTxt)
    -- ...

return module
-- Script or LocalScript
local myModule = require(script.Parent:WaitForChild("ModuleWithCode"))

Interestingly, when I test this example in Roblox Studio, the Output-panel actually does show - with red error - at which line in the modulescript that cause the failure, besides the require error and its stack-trace.

The actual problem isn’t very relevant to the posting, but if you must know, it was related to attempting to use DataStores in Studio at a time when the Roblox was handling such requests rather poorly.

For the record, all the code is my own. And yes, that’s what I did to solve the error that I mentioned previously. I’ve also mentioned that I’ve used print() statements in the past to debug, and ultimately I can find the errors. The problem here is not how to fix the errors, the problem is how to get details about the error.

The difference between your code and mine is that your code is contained within a function. That works fine for getting error details, as you’ve seen. The problem is when the code is contained outside of the function. Like if you were to do this for example

Regular Script code

moduleScript = require(script.ModuleScript)

ModuleScript code

local module = {}

workspace.PartThatDoesntExist.CanCollide = false

return module

It would tell you that the module experienced an error on loading, not that PartThatDoesntExist is not a valid member of workspace.

Actually in writing this out, I think I may have come up with a solution. I’ll post back if it’s successful.

I’ve got it. My solution is to wrap all the code that isn’t part of a function (i.e. code that will run on require) into a single function called ‘module:initialize()’ and then I call that function immediately after requiring the module. That gives me the error output I was looking for.

Thanks for all the suggestions.