Table.remove is removing items from 2 different tables

I want to delete items from a table based on another value, this part of the code is already working, but imagine that I have a map using matrix (Map[x][y]) and inside Map[x][y] there are many values like the “allValues” below. So I copy it and search for specific numbers so I can remove them.

This is just an example, but it shows what I meant when I said “removing from 2 different tables”

local allValues = {1, 2, 3, 4, 5}
local allValuesSon = allValues

print("allValues:", table.concat(allValues, " "))
print("allValuesSon:", table.concat(allValuesSon, " "))

table.remove(allValuesSon, 2)

print("allValues:", table.concat(allValues, " "))
print("NewAllValuesSon:", table.concat(allValuesSon, " "))

It results in:

allValues: 1 2 3 4 5
allValuesSon: 1 2 3 4 5 

allValues: 1 3 4 5
NewAllValuesSon: 1 3 4 5

Why does it remove the values from “AllValues” too?

1 Like

Because when you set it, it is simply a reference to the same memory space. You can use table.clone.

Wow, ty. I didn’t know tables behaved like that.

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