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
task.wait(5)
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.
Data = {}
local module = {}
function module:CreateDataStorage(Name:string)
local DataManipulation = setmetatable({},{})
if not Data[Name] then
Data[Name] = {}
end
function DataManipulation:SetAsync(Key:string,Value:any)
Data[Name][Key] = Value
end
function DataManipulation:GetAsync(Key:string)
return Data[Name][Key]
end
return DataManipulation
end
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.
“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.”
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.
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.
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
New_Library = {apple="Tasty"} -- This is now a shared library.
module = {}
function module.Get()
return New_Library["apple"]
end
function module.Set(value)
New_Library["apple"] = value
end
return module
The subsequent code allows for the sharing of values between modules through a getter/setter methodology.