When you declare a variable as a table’s index, that variable is stored (cached) even after said index no longer exists in the table.
To set it to nil, simply set said variable to nil.
local table = {
["item"] = "axe",
["rarity"] = "common"
}
local rarity = table["rarity"]
print(rarity) -- "common"
print(table["rarity"]) -- "common"
table["rarity"] = nil
print(rarity) -- "common", because it's cached
print(table["rarity"]) -- nil, because we removed it
rarity = nil
print(rarity) -- nil, since we set the variable to nil
Actually, tables are pass by reference and not pass by value. The way it works is intended. You need to erase all references by pointing them all at nil. Treat them as pointers and you will be fine.
That’s probably a question that should be in a separate post, but you need to copy everything in the table to a new table:
local deep_copy function deep_copy(tab)
local copy = {}
for k, v in tab do
if type(v) == "table" then
copy[k] = deep_copy(v)
else
copy[k] = v
end
end
return copy
end
local table1 = {1, 2, 3, 4, 5}
local table2 = deep_copy(table1)