Weird table behaviour

I’m setting a table to a default table yet it seems to have no change.

When I index the table I want as seen in the code below it resets that table and it works.

islandDataTable[proximity.Parent.Parent.Name] = default_island_variable_table

However if I were to use this code, it doesn’t work:

local islandTable = islandDataTable[proximity.Parent.Parent.Name]
islandTable = default_island_variable_table

FYI, the tables are both the exact same I’m pretty sure. I’m just unsure as to why it isn’t resetting when I use the second way.

islandTable is a variable that stores the value of islandDataTable[proximity.Parent.Parent.Name]. It does not store the memory location of islandDataTable[proximity.Parent.Parent.Name]. This means any changes to the variable do not affect the table. To change a value in the table you have to use the first piece of code you provided

1 Like

this is because setting a variable to an already existing table doesn’t make it a different table like a number or a string would instead it’s only a reference to that table.
For example

local a = {}
local b = a

print(a, b) --> table: 0xcc4362a8c75f31df  {}  table: 0xcc4362a8c75f31df  {}

both print the exact same memory addres because they both refer to the same table.
To fix your issue you can use table.clone to make a copy of your default table.
note that table.clone is only a shallow copy so it won’t copy a nested table.
If your default table has nested tables (a table in the table) then you would want to deepCopy your table.
You can find more info here

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.