Consecutively requiring a ModuleScript from the Command Bar does not re-execute the script after modification

The result of a required ModuleScript will “cache” after being called by the command bar in studio. This may be expected behavior, however this “cache” does not reset after modification of the script.

Example:

-- Code for game.ServerScriptService.ModuleScript
local module = {}
function module:foo()
    print("Hello, world!")
end
return module

-- Code executed on Studio's Command Bar:
local moduleScript = game.ServerScriptService.ModuleScript
require(moduleScript):foo() --> Hello, world!"

-- Edit code for game.ServerScriptService.ModuleScript:
local module = {}
function module:foo()
    print("Goodbye, world!")
end
return module

-- Code executed on Studio's Command Bar:
local moduleScript = game.ServerScriptService.ModuleScript
require(moduleScript):foo() --> Hello, world!"
                            ^ INCORRECT OUTPUT

This makes testing code you may have written in a ModuleScript return unexpected results, as it would be reasonably assumed by a developer that if you make a change to a Module and re-require it by the Command Bar, you are testing the new code.

This happens both when calling require multiple times in a single command bar execution, or when calling it again later in a separate command bar execution.

Workaround

A workaround I’ve found for this is to instead require a clone of the ModuleScript each time you wish to execute it, however this is obviously not ideal nor a permanent solution.

4 Likes

This is a pretty simple solution:

-- Module code
return function() 
     print("Hello, world!");
end
1 Like

This is not a solution at all.

  1. It does not solve the original problem of code in the script not executing. I should not have to change the entire layout of my module script to return my desired code within a function just for it to work.
  2. If you make any change to the function returned by the module script, that change will not register. Studio effectively “caches” the result from the first module script require.

Example:

-- Module code
return function() 
     print("Hello, world!");
end

-- Command Bar Code
require(moduleScript)() --> Hello, world!"

-- Edit Module code
return function() 
     print("Goodbye, world!");
end

-- Command Bar Code
require(moduleScript)() --> Hello, world!"
                        ^ INCORRECT OUTPUT

The same happens if you’re returning an entire table of functions (which is the most common return of a ModuleScript). If you add new functions to the table and try to call them, you will get an error. If you try to modify the code within these functions, it will not register the change.

2 Likes

It is unexpected, but without this unexpected hiccup, modules would be paperweights.

local Table = {}
return Table

you can change this, right?

print("Hello, world!")
return true

if this ran every time you required it, so would the top example, meaning that state would not be, and state is the entire point of modules.

local Module = {}
function Module.Function()
    print("Hello, world!")
end
return Module

this would do the desired thing, while also being editable.

1 Like

The core of the issue is that it is in fact not editable:

-- Module code
local module = {}
function module:foo()
    print("Hello, world!")
end
return module

-- Command Bar Code
require(moduleScript):foo() --> Hello, world!"

-- Edit Module code
local module = {}
function module:foo()
    print("Goodbye, world!")
end
return module

-- Command Bar Code
require(moduleScript):foo() --> Hello, world!"
                        ^ INCORRECT OUTPUT

I agree, the entire point of a ModuleScript is to run the code a single time when called from multiple locations, and cache the result so that from different locations re-requiring is efficient as it doesn’t re-execute the code. However, I would argue that in Roblox Studio, the Command Bar primarily exists for the purpose of debugging and testing code you have written. How are you supposed to test the code you have written in a ModuleScript if studio does not recognize the change you have made to the script?

That is truly the core of my issue. I will admit that my initial code example was weak and over-simplified compared to the problem I was experiencing. I will edit my original post to include a more detailed code example of this exact problem.

1 Like

Thats the same exact issue i faced

my solution was to save place to file then close file and re-open the file i know this sucks but theres also 2nd solution

2nd solution is just press “reload script” in scripting tab once you are focused to that modulescript if you want studio command bar to recognize changes that you made to the modulescript

1 Like

if you still don’t understand me because of my poor wording here is video footage of me doing it live

I hope i satisfied your needs

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.