I wasn’t exactly able to find this question anywhere else but…
I have experimented with module scripts and noticed something odd.
Let’s say we have scriptA and scriptB.
Inside a ModuleScript we have
local mod = {}
mod.testvalue = 1
return mod
When I increase testvalue by 1 with scriptA, scriptB will immediately see it and be able to print it in the output.
scriptB will just print mod.testvalue > 2
in the output.
But when I do this
local mod = {}
mod.testtable = {}
return mod
Now I add a value/another table to testtable in the module with scriptA.
let’s say I do something like
local mod = require(path.to.module)
mod.testtable["testvariable1"] = {"string", true, 1}
mod.testtable["testvariable2"] = 12345
When I do #mod.testtable
in scriptA it prints 2 (the table length)
and when I do a ipairs loop for example, it prints all values, etc as supposed to be/do.
But whenever I do this with scriptB, #mod.testtable
prints 0 (despite table having values in them)
and when I do a ipairs loop it all pretends and behaves as if the table is empty.
I find this very strange.
And I’m completely confused by
why number values updated by scriptA show up in scriptB.
But why do table updates/new values added by scriptA not show up in scriptB?
Is there a explaination for it and a solution?
I might want to save NPCs and their variables in a single global module script and have other scripts read from it, etc without re-using require() (I only want to use require() once per script, and even re-requiring didn’t work in some cases btw).
Should I use _G instead if I want a global table containing NPC data or such that update real time?
Modules apparently act weird if you add new tables and values to them, scriptA can see them, but scriptB and other scripts don’t seem to notice the changes.
(P.s, please tell me if I’m in the wrong topic or if this question was asked before, I was unable to find it, also tell me if something is unclear, I tried my best at explaining my issue.)