Removing elements from a table

Hello,
I am trying to make an enemy system where all enemies are represented as tables and stored server side

I have an issue where when an enemy dies I remove it from the table of enemies that are stored then that table is put through enemy logic but for some reason the enemy that was supposed to be still deleted is still in the table and throws an error for attempting to index nil

I’ve seen a similar post to where someone was experiencing a similar issue which they solved by reversing the iteration of the table, I’ve tried to do the same but it still says attempting to index nil

Part of the script that handles the table


local count = 0
	for _, index in toRemove do
		print(count)
		
		local indexToRemove = #toRemove - count
		local remove = toRemove[indexToRemove]
		currentEnemies[remove].visual:Destroy()
		table.remove(currentEnemies, remove)
		print("removed")
		count += 1
	end

If you’re wandering this is client sided

uh, can’t you just remove the enemy from the table upon it dying? unless that’s not what you’re looking for

you are using table.remove without using its right parameters, just do a loop that goes over every item stored in the table with a for _, x () loop and if x == toremove then just get the index from the loop and do table.remove(table, _)

Ok I’ll edit the post but the enemy logic runs every .1 seconds (server side) then once it processes all the enemies it sends a table to the client containing data about every enemy this is then processed on the client side to remove it from the table of existing enemies to stop it from being visually rendered if it is supposed to be dead

for i = #toRemove, 1, -1 do
	local remove = toRemove[i]
	currentEnemies[remove].visual:Destroy()
	table.remove(currentEnemies, remove)
end

I would say you should just parent the enemies to a folder named “Enemies”, use getchildren on the folder to access the enemies. And whenever the enemy dies, destroy the model automatically after 3 seconds by adding a simple event to each enemy.

I think bytesleuth has it, reverse the iteration.

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