Why does a ModuleScript requiring a specific returned value from another ModuleScript not update when reloading the script?

Hello, to explain my issue, I will show you an example.

I created two ModuleScripts inside “ServerScriptService”
image

Here’s the Source Code for each:

ModuleScript “Test”

local TestModule = {}

TestModule.testValue = require(script.Parent.Test2)

return TestModule

 

ModuleScript “Test2”

local TestModule2 = {}

TestModule2.testValue = "Hello"

return TestModule2.testValue

 

So recreate these scripts, then open the Command Line in Studio, and type this code.

local test = require(game.ServerScriptService.Test) print(test.testValue)

This will print out “Hello”

 

Now, update the ModuleScript “Test2”
Change the “testValue” to something else, like
TestModule2.testValue = "Text updated"

then click on “Reload Script”
 
and then run this command again:

local test = require(game.ServerScriptService.Test) print(test.testValue)

 
What I expected, was that it prints out “Text updated”, however it printed out “Hello”.

And if I reload the the script that requires “TestModule2”, it still won’t update “testValue”.

Why does it not update?

The same thing goes for, when a script runs a function that was declared in a module script, which gets required by another module script and then a function gets run to replace the value, however it doesn’t update, as well, only if the function is from the first required ModuleScript from the script.

2 Likes

From my own experiments in the past I noticed modules utilize caching so updating it in the source won’t change by default unless you duplicate the module and then require it again or you reload the place.

That is the case for using it from the command bar mainly, I’d recommend running it from in game I guess.

One of the ways I got around this (in game, haven’t myself found a workaround other than the duplicating / reloading place for the command bar) was returning a function that returns the module you want.

This has been a bug since 2014 at a minimum, I’m not sure if it’ll be fixed. If it’s absolutely needed, you could make a function that loadstring()‘s the module’s source with an environment for the ‘script’ variable.

3 Likes

Was this on purpose? Why create a module just to return a single property value from it instead of returning the whole module?

At the top of the post, it said a test example relating to the issue. I’m presuming they’re utilizing modules for something similar but didn’t know if it was their code or not so ran this test separately and experienced the same results.

If we knew more about what they were trying to accomplish we may be able to offer an alternative. It doesn’t make sense to me to return a single module value in this manner.

I’m guessing they’re testing a module in their game from the command bar and ran into this hiccup. Although, I do agree that it would help for more details.

Not on purpose, just to recreate the issue.

If you return the entire Module

do something like

TestModule.TestModule2 = require(script.Parent.Test2)
TestModule.testValue = TestModule.TestModule2.testValue

and then do the same thing, it would still not update the string.

I see what you mean. Studio doesn’t seem to update the changes your making to module scripts unless you forcibly reload them.

It’s not only the command line, even if I do that what I did in the command line, inside a script, the same issue occurs.

Unless the value gets updated by the first ModuleScript returned with require() and not from a dependend ModuleScript of the first ModuleScript returned with require()

Well, “Reload Script” doesn’t reload them either, doesn’t matter if first or second one was reloaded.

I actually have to re-open the experience in Studio.

Launching the game though, I think on one part it updates, however as soon as closing the Test Mode, the values are still not updated.

But then whatever gets updated there, doesn’t update.

I was able to get the new information by reloading both scripts.

testValue = "Hello"

TestModule2.Change = function(str)
   testValue = str
end

I ran this test, because I was trying to use something like that, out of curiousity. But it’s not about returning one single thing, even if I return the entire table and change the ModuleScript “Test” to this

local TestModule = {}

TestModule.TestModule2 = require(script.Parent.Test2)

TestModule.testValue = TestModule.TestModule2.testValue

return TestModule

just to have “testValue” under TestModule and not TestModule.TestModule2.testValue
while, I might think that this could have been done differently

My purpose with that, was to try to split an entire codeblock into multiple smaller files and require them, however the issue is that the functions don’t update

I’ve tried something similar, but it only worked if the function was in ModuleScript “Test”, so “TestModule”

and if I declared testValue under TestModule from TestModule2, I think

what i posted 100% will change the variable to whatever you declare the str, aka anything including functions, dictionaries of tables of functions, havent properly checked for reading but im p sure u also need to do smth like when ur changing it constantly, this is what i usually do anyway cos i dont have the time to learn all the rigid rules of rlua, if it works it works

var = {}
TestModule2.Change = function(key,value)
   var[key] = value
end
TestModule2.Read = function(target)
  return var[target]
end

alright, though I think here I have to change TestModule2 to return “TestModule2” and not “TestModule2.testValue”

but I am not sure if this is gonna be good with functions inside of “TestModule2”

but what I am trying to point out to is, the values update when the game starts, or when restarting the entire Roblox Studio

why doesn’t it update on Reload Script?

Could it be a Roblox Studio issue?

TestModule2 = require(script.Parent.Test2)

local TestModule = {}

TestModule.ChangeModule2Table = function(key,Value
   TestModule2.Change(Key,Value)
end


return TestModule

Even something like this

local TestModule2 = "hi"

return TestModule2

will not properly update in the Roblox Studio Editor

If I change ModuleScript “Test”

to this

local TestModule = {}

TestModule.TestModule2 = require(script.Parent.Test2)

return TestModule

with the code from above for “Test2”, let’s say I just created the ModuleScripts, it would “hi” when doing

local test = require(game.ServerScriptService.Test) print(test.TestModule2)

if I however, change it to local TestModule2 = "Text updated"

and print it again, it still prints “hi”

if I do this though TestModule.TestModule3 = require(script.Parent.Test2)

and do print(test.TestModule3) it will print the one that it updated to, however, then the same issue occurs for that variable as well

Why do you want that to work in the first place. The exact same thing can be achieved in several different ways and you’re just wasting a module script for a singular variable. I don’t know why modulescripts require that sort of workaround but unless its going to affect my games performance or is causing a bug I will live with what works

The code mentioned here, is just an example, for others to test.

Again, the code works fine. The issue is, when you change the code in Studio, it doesn’t update, you have to re-open the game.

I think this is a Studio issue, but I am not sure.