nestedtable = { a nested table with more tables inside which hold values etc }
dictionary = {
a = {any},
b = nestedtable
}
wait(10)
clearallitemsintables( nestedtable )
print(dictionary)
In a dictionary you can clear out a key,value pair by setting the key to nil.
So you can remove the reference to the nested table âbâ by setting dictionary.b = nil.
If you then looped through dictionary, b wouldnât show up.
If when you set b to nil, b was the last reference to it, then the table will be cleaned up. If you still have a reference (nested table would be a reference) then the data will stick around, just no longer accessible from the dictionary. Otherwise if b was the last reference the data would get cleaned up.
So at the end of your code when you set nested table to a new table, the original reference remains in dictionary.b making that the only reference. So itâs still accessible to dictionary but no longer to accessible to nestedTable.
You can just go through the table and search up for it and after that remove it!
for i,Table in (tablewhereyousearch) do
if Https:JSONEncode(Table) == Https:JSONEncode(tableThatYouWantDelete) then
table.remove(tablewhereyousearch,i)
end
end
Ok. My first attempt was hard to read, so replying to this will hopefully be better.
To start, your code is creating a reference nestedTable to a table.
That means the table now has 1 reference
Then you set dictionary.b to the table as well. Now the table has 2 references.
Then you change what data nestedTable is pointing to. It now points to a new table removing the reference to the old table. So the original table now has 1 reference (dictionary.b). Since there is still a reference the data will not be cleaned up. To remove the reference from dictionary, you would set dictionary.b to something else (or nil to remove dictionary.b entirely)
It creates an empty table, meaning the variable has lost everything it had stored previously. The reason you set it to an empty table is because you canât set it to nil. It canât exist in memory without a reference. Garbage collection cleans everything that is nil and/or without reference.
then this { workspace.Chair = { Price = 250, Name = âHappyâ } } continues to exist meanwhile a new table was created for Table, I didnât set the previous table to empty, I created a new table and set it to the âTableâ variable which now refers to a new table rather than the previous one.
The way I would do it is.
Table = DeepClean(Table) â which goes through the entire table and removes all the keys and essentially makes the table empty.
Once the table is empty then it can be garbage collected.
What makes you think it still exists in memory? Also the only references you need to worry about cleaning are object references, that way they can be garbage collected when destroyed or parented to nil.
table.clear sets all keys to ânilâ, although it doesnât perform a nested clear.
local function nestedClear(t)
for i, v in pairs(t) do
if type(i) == "table" then
setmetatable(i, nil)
nestedClear(i)
end
if type(v) == "table" then
setmetatable(v, nil)
nestedClear(v)
end
end
setmetatable(t, nil)
table.clear(t)
end