You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I’d like to access a table defined inside of a ModuleScript and modified by another script at runtime from a third script.
What is the issue? The table returns full when it’s printed from the first requiring script, but executing another requiring script results in an empty table.
What solutions have you tried so far? I haven’t found any solutions on the Forum or Creator Hub, as any other post mentioning this .
I’ve been writing a lighting controller script for a game that I’m developing, and the following is the relevant portions of the ModuleScript:
type MhbImpl = {
__index: MhbImpl,
new: () -> Mhb
}
export type Mhb = typeof(setmetatable({} :: {
-- data fields irrelevant to question here
}, {} :: MhbImpl))
local Mhb: MhbImpl & {cache: {Mhb}} = {} :: MhbImpl & {cache: {Mhb}}
Mhb.cache = {}
Mhb.__index = Mhb
Mhb.new = function()
local self = setmetatable({
-- irrelevant fields here
}, Mhb)
table.insert(Mhb.cache, self)
print(Mhb.cache) -- works as expected, prints a full cache
return self
end
return Mhb
Then a second script:
for i=1,10 do
Mhb.new()
end
print(Mhb.cache) -- also works as expected, prints a full cache
And then a THIRD script:
print(require(path.to.module.on.server).cache) -- prints an empty table (you can just run this in the command bar on server)
What’s going on here? The idea behind the cache is to present a list of all of the objects to other scripts for external manipulation, so this has me stumped.
Maybe the third script is being ran before the second on resulting in it printing an empty table, so try adding an task.wait in the third script to see if it print something else then a empty table.
from what i can see you’re not returning the module in the first place? the only return statement i see is the return self in Mhb.new()
But since it was working correctly in the second script im guessing you have the return statement but didnt include it here, for some odd reason.
Anyways i got a few questions,
Are you running the command bar code during playtest?
Are you sure you switch to server before running the command bar code?
I am not experienced in how garbage collection works but maybe there is a chance all the items that get created get garbage collected once script 2 finishes? but that feels unlikely.
Anyways i have to go now, ill get back here in about an hour
You should try running with 2 scripts instead of running with 1 script and one command bar execution.
If it is correct that the command bar has a seperate environment then that should fix your issue