Problem with arrays in a modulescript

I want to modify values in a modulescript array using the first script and modify them in another one.

When I’m saving data using table.insert in the first script and requiring the modulescript again in the other one, it’s not saved.

I’ve tried looking for a way to save and load big tables containing instance information, I’ve found modulescripts are the way to do it properly, as I didn’t want to use _G variables as people said they tend to be buggy and sometimes not work.

-- modulescript:
local module = {}

return module

--first script:

local module = game.ServerStorage.ModuleScript
local moduleTable = require(module)
table.insert(moduleTable,#moduleTable+1,"Data")

-- second script:
local module = game.ServerStorage.ModuleScript
wait(1)
local moduleTable = require(module)
print(moduleTable[1])

Expected output:

“Data”

Actual output:

nil

Am I doing something wrong? How can I make it print out the expected result?

EDIT: nvm this all works and the expecte output is the same as actual output, i was doing something wrong

That’s because Module Scripts are programmed to work not as numeric tables, but as generic ones where you have a key and a value associated with that key.

In your case, you might want to replace that table.insert line and manually locate (create in this case) the key and assign a value for it, either by using the dot notation or the square brackets notation.

Dot:

moduleTable.Data = "value"

Square brackets:

moduleTable["Data"] = "value"

And when you access it in the second script, you just do moduleTable.Data or moduleTable[“Data”].

1 Like