"Module code did not return exactly one value" error message not caught by pcall

  1. Make a new ModuleScript in workspace.
  2. Delete the return statement.
  3. Run the following code in the command bar.
    print(pcall(require, workspace.ModuleScript))
  4. pcall will correctly catch the “Module code did not return exactly one value” error message.
  5. So far so good, now press play and run the same code in the command bar again.
  6. pcall will fail to capture the error message, although the error itself will be correctly caught.

Expected behavior is that pcall traps error messages regardless of whether the game is running.

Also, the error message returned by pcall in both cases should be “Module code did not return exactly one value”, but it actually returns the vague “Requested module experienced an error while loading”.


It’s probably because the ModuleScripts do their first load on a separate context.


Shouldn’t it still distribute to the correct context level though?

That shouldn’t depend on whether the game is running.

Output for me: false Requested module experienced an error while loading
It does print 12:07:23.917 - Module code did not return exactly one value in red though.
Whether I do it with the game running or not, I always get the same result, which is to be expected.

No, if you require them from a context with identity 4, they’ll run in identity 4.
You can even run a modulescript several times at once, e.g. one for each identity.

You probably mean that the modulescript does their stuff on a separate coroutine. (“thread”)
require() is like “The return value is the internal value for ERROR, let’s error the default error message”.

Only “fix” I can think of is changing require() to behave like pcall, returning a boolean + result/error.
(although now it’s too late to change how require() behaves, but maybe create a p_require()?)
(I mean, would make more sense to name it prequire but that reminds me of the word “prequiring” etc)