I can’t really say for certain what the problem is, but I can take a couple of guesses and toss out a bunch of info.
To create a completely new, non-referenced copy of the table, you’ll need to create a deep copy of the table if it’s multiple dimensions deep. That link is also a perfect example of recursion (a function calling itself) and can save a lot of time and headache if it’s used correctly, most importantly it’s scalable.
It’s easier to think of everything in the form of tables when dealing with modules instead of something completely new; Mainly because they are a table and so they behave exactly like one:
Module Script
local module = {
Table = {5,4,3,2,1}
}
function module.SomeFunction()
end
function module.SomeOtherFunction()
end
return module
Script
--Iterating through a module (not sure of any use cases for this though haha)
local exampleModule = require(script.Parent)
for i,v in pairs(exampleModule) do
print(i, v)--Prints Table and its contents, the two functions as well as their memory location
end
I’ve created some quick examples of modules that showcase some of the traps as well as more examples of modules behaving very table-y:
Module Script
local module = {
Table = {"a", "B"};
PrintThisOut = function(printRequest)
print(printRequest)
end;
}
local someOtherTable = {5,4,3,2,1}
function module.ReturnTableReference()
return someOtherTable
end
return module
Script
--Requiring a module gives a reference to everything located inside the module's table:
local exampleModule = require(script.Parent)
print(exampleModule.Table[1]) --prints a
exampleModule.Table[1] = nil --removing a key/value from module.Table in the other script
print(exampleModule.Table[1]) --prints nil
--Clearing the memory of a reference only affects the reference:
exampleModule = nil --ONLY removes the reference, keeps exampleModule intact
exampleModule = require(script.Parent) --reference the module again
print(exampleModule.Table[1]) --it really did remove it from earlier!
--Accessing a function in the module based off of its key:
exampleModule.PrintThisOut("Example") --prints Example
--Returned tables are also references:
local referencedTable = exampleModule.ReturnTableReference()
referencedTable[1] = nil
referencedTable = nil --ONLY removes the reference, keeps "someOtherTable" intact
referencedTable = exampleModule.ReturnTableReference() --call the function again just to prove it's the original copy :)
for i,v in pairs(referencedTable) do
print(v) --removed the first key which held the value 5 from the table of "someOtherTable"
end
I hope one of these solves your problem. Good luck!