Module script doesn't replicates values between scripts

I was experimenting with dynamic storage. With my understanding, module scripts are way better than _G or shared global variables. But when I tested, I found out that if you set values on module scripts, they won’t replicate to other scripts that require this module script. Here is an example:

ScriptA: - ServerScript

require(game.ServerScriptService.Module)["TestValue"] = 5
print(require(game.ServerScriptService.Module)["TestValue"]) -- prints 5

ScriptB: - ServerScript

print(require(game.ServerScriptService.Module)["TestValue"]) -- prints nil

I tried different ways of indexing, but they didn’t work. Either Module[1] or Module.TestValue. So

Is there any way to dynamically replicate stuff with module scripts?

ModuleScripts cannot be written to, as far as I know. When you require() a ModuleScript, it just returns what the ModuleScript returns, it doesn’t create any tunnel to edit the contents inside of it. So I’m afraid you’ll have to use _G.

Try changing the value without requiring the module.

no, module scripts can store the values and give the values when required from another script.

What? This is incorrect. OP, your code should be working fine

Can you send your module script cus as @Uzixt said and from what I have tested, the code should work fine.


Data = {}
local module = {}

function module:CreateDataStorage(Name:string)
	local DataManipulation = setmetatable({},{})
	if not Data[Name] then
		Data[Name] = {}
	function DataManipulation:SetAsync(Key:string,Value:any)
		Data[Name][Key] = Value
	function DataManipulation:GetAsync(Key:string)
		return Data[Name][Key] 
	return DataManipulation

return module

A ModuleScript is a script just like any other script would be. You can put anything in it, and it will execute. The catch is that a ModuleScript can and should return some value, most commonly a table, but it can be a number, a function, anything, and that returned value can be retrieved by require()ing it. You cannot modify what that script returns if you don’t overwrite its source.

that’s not how Module scrips work

If a ModuleScript returns a table, you can very well modify the contents of the table (unless I’m misunderstanding you?)

“A ModuleScript is a type of Lua source container that runs once and must return exactly one value. This value is then returned by a call to require given the ModuleScript as the only argument. ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require.”

I ment this is not how it works.

In my previous post, quoting the docs:
“and return the exact same value for subsequent calls to require

When you require a module script, what you are essentially doing is cloning the repository of code.

If you use the module as a getter/setter methodology, which sets an internal dictionary which isn’t returned to the user then this functionality is possible.

_G for example, is a pointer to a fixed module shared by all scripts, this is why it works here. You do not require _G.

1 Like

Yes, finally someone that understands my point. And yeah, making a get/set function would make this ModuleScript storage method possible, which I did not take into account (but still implies that ModuleScripts can run).

Well my only disagreeance to your point is “_G is the only module that can do this”, when frankly any module script can do this providing the architecture supports it.

1 Like

I never wrote that _G is the only method that can do this replication, I just didn’t write that ModuleScripts can have functions to get/set values and that’s my fault. But I agree with you.

I might also cough up the problem. And it seems to be how Lua compiles non-string indexers. It doesn’t work if you point at them without a string. but works fine when you index them with strings. It’s a bit weird, but it’s probably how Lua compiles the code. not sure, though.

EDIT: Nevermind, there is something off with my data module.

ModuleScripts only run once when require’d, returning a reference
If you modify anything, any subsequent calls will return the modified reference
You don’t necessarily need a getter/setter system, since every time you access the modulescript it accesses the reference. Do note, however, if you require the module from both the client and server it creates a separate reference

Here is how the behaviour should look:

New_Library = {apple="Tasty"} -- This is now a shared library.
module = {}

function module.Get()
   return New_Library["apple"]

function module.Set(value)
   New_Library["apple"] = value

return module

The subsequent code allows for the sharing of values between modules through a getter/setter methodology.