Item doesn't get removed from table when certain conditions are met

Soooo I’m making a tower defense game and did more enemy system stuff YAY (help).

What I am doing is adding the enemy on the server as “data” (including its name, health, speed) etc to a table. Then I check if the enemy reaches the exit or dies I remove it from the table. Problem is it DOES NOT get removed…

This is the entire enemy movement part, where you can see I add the enemy to a table.

	task.spawn(function()-- append enemy to the current enemies table to only exist as data
		
		for i=1, amount do -- run for amount
			
			local Positions = {}

			for waypoint = 1, #workspace.Main.Map.Waypoints:GetChildren(), 1 do
				table.insert(Positions, workspace.Main.Map.Waypoints[waypoint].Position)
			end
			
			task.wait(succesion)
			
			local startTime = workspace:GetServerTimeNow()
			
			--table.insert(enemies, enemy) -- append
			
			local NewPath = bezierPath.new(Positions,3)
			--local totalTime = NewPath:GetPathLength() / list[enemy].Speed
			
			local newFolderForEnemy = Instance.new("Folder") -- create a new reference object
			newFolderForEnemy.Name = enemy 
			newFolderForEnemy.Parent = workspace.Main.CurrentEnemies
			
			--local enemyTable = enemies.enemy
			
			local enemyTable = {
				
				["Enemy"] = enemy,
				["ID"] = id,
				["Health"] = list[enemy].Health,
				["Speed"] = list[enemy].Speed,
				["Position"] = CFrame.new(0,0,0),
				["ReferenceObject"] = newFolderForEnemy
			}
			
			spawnEnemyModule.Entities[id] = enemyTable
			id += 1
			
			local loop 
			warn(spawnEnemyModule.Entities)
			
			loop = runService.Heartbeat:Connect(function()

				local totalTime = NewPath:GetPathLength() / enemyTable.Speed
				--local t = (workspace:GetServerTimeNow() - startTime) / (totalTime)
				local t = math.clamp((workspace:GetServerTimeNow() - startTime) / (totalTime), 0, 1)
				enemyTable.Position = NewPath:CalculateUniformCFrame(t)

				if enemyTable.Health <= 0 then

					loop:Disconnect()
					enemyTable.ReferenceObject:Destroy()
					spawnEnemyModule.Entities[id] = nil

				end

				if t >= 1 then -- remove enemyReferenceObject and loop once last waypoint is reached
					enemyTable.ReferenceObject:Destroy()
					spawnEnemyModule.Entities[id] = nil
					loop:Disconnect()
					workspace:SetAttribute("health", workspace:GetAttribute("health") - enemyTable.Health)
					--warn("Server: "..workspace:GetAttribute("health"))
					updateBaseHealthVisuals:FireAllClients()

					if workspace:GetAttribute("health") <= 0 then
						spawnEnemyModule.clearEnemies()
						gameOverBool.Value = true
					end

				end
					
				table.insert(connections, loop)
				
			end)
			
			function spawnEnemyModule.clearEnemies() -- make so it clears all loops
			
				table.clear(spawnEnemyModule.Entities)
				workspace.Main.CurrentEnemies:ClearAllChildren()
				
				for i, v in pairs(connections) do
					v:Disconnect()
				end
				
				table.clear(connections)

			end
		end
	end)
	
	createEnemyEvent:FireAllClients(stream, enemyID, amountOfSpawns, successionOfSpawns, startTime) -- fire remote to create funnehe enemy

end

You can also see that I am removing the enemy from the table (setting to nil) in the runService loop.

But instead of removing it just keeps the enemy inside the table and only “nils” the position variable

(This is an enemy which entered the base)

1 Like

Yes, I really need help please

1 Like

you are adding 1 to the id so its removing the current + 1 use enemyTable.ID

1 Like

Why would it be removing the +1 id? (“id” is a. global variable with the value 1)

1 Like

spawnEnemyModule.Entities[id] = enemyTable
id += 1

2 Likes

That’s what I have been doing the entire time… Look into the script I send and you’ll see

I understood now, thank you very much

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