Table is removing random Variable

I tried something:

local Table = {}

print(Table[1])

table.insert(Table, "1")

print(Table[1])
	
table.remove(Table, table.find(Table, "2"))

print(Table[1])

This will print:

nil

1

nil

If thee 2 is not in the Table, it will remove something random in the Table, but why?

There is no 2 inside the table nor are you inserting it anywhere in your script. If table.find can’t find the entry it can’t return an index which would remove nothing.

So if there is no 2 it removes just something inside?

Since you started the table at the top of the script as empty, once you removed the only entry in it which was “1” the table was empty. It didn’t remove anything because there was nothing to remove.

But im first inserting the 1 to the Table, then im removing the 2, what is not removing the 2 but the 1

From what testing is showing, when table.find returns nil, table.remove() uses table[#table] which deletes the last entry in the table.

local Table = {"3","4","5"}

print(Table)

table.insert(Table, "1")

print(Table)

table.remove(Table, nil) -- Replcaed for testing, table.find(Table, "2") returns nil

print(Table)

Output:
image

1 Like

Thanks i also thought that, but was not sure