Finding what number an instance is in a table

Quick question, I hope.

I have a table of Enemies, and when one “dies” I want to remove it from the table. To remove an instance from a table you need to have the number of the instance in the table. How would I keep track of this?

Assuming the table is like

{
    [1] = enemy1;
    [2] = enemy2;
    etc.
}

You can use table.find(table, value) to get the index. For example:

local enemies = {
    [1] = enemy1;
    [2] = enemy2;
    [3] = enemy3;
}

enemies[table.find(enemies, enemy1)] = nil
1 Like

Adding onto Orbular’s reply, table.find returns the index number where a supplied value is found, if it finds none it returns nil, definitely useful.

Yes, but due to it being a Tower Defense game, the same type of enemy (with the same name) will spawn multiple times.

So the table could be like

[1] = enemy1;
[2] = enemy2;
[1] = enemy1;
[2] = enemy2;
etc.

Would (table.find) still work in this situation with the same name being repeated twice?

Are you storing names as values or instances? If the former then switch to the latter.