Removing a clone from a table

Hello all! I have run into quite the dilemma. I have created clones of an object and each object created has either a value of “Red” or “Blue” depending on which team they are on:

for i = 1, scorchSpawnLimit do
table.insert(redTeamMinionsLane1, redScorchMinion:Clone())
table.insert(blueTeamMinionsLane1, blueScorchMinion:Clone())

	-- Set the primary part to head
	redTeamMinionsLane1[i].PrimaryPart = redTeamMinionsLane1[i].Head
	blueTeamMinionsLane1[i].PrimaryPart = blueTeamMinionsLane1[i].Head

	redTeamMinionsLane1[i].Name = "Minion"	
	blueTeamMinionsLane1[i].Name = "Minion"		

	-- Move the primary part to the spawn
	redTeamMinionsLane1[i]:SetPrimaryPartCFrame(redMinionSpawn1.CFrame * * i, 0, 0))
	blueTeamMinionsLane1[i]:SetPrimaryPartCFrame(blueMinionSpawn1.CFrame * * i, 0, 0))

	-- Assign to lane
	redTeamMinionsLane1[i].Head.Lane.Value = "CheckPointsLane1"
	blueTeamMinionsLane1[i].Head.Lane.Value = "CheckPointsLane1"
	-- Assign to team
	redTeamMinionsLane1[i].Head.TeamValue.Value = "Red"
	blueTeamMinionsLane1[i].Head.TeamValue.Value = "Blue"
	minionScript:Clone().Parent = redTeamMinionsLane1[i]		
	minionScript:Clone().Parent = blueTeamMinionsLane1[i]
	-- Move the model to workspace
	redTeamMinionsLane1[i].Parent = game.Workspace
	blueTeamMinionsLane1[i].Parent = game.Workspace
	_G.minionCount = _G.minionCount + 1



There is more code, but I believe this will suffice. Each object also has a health bar. Whenever the size of the health bar reaches (0, 0, 1, 0), I want the game to destroy the clone and remove it from the list according to its team.

I know the table.remove() function takes two parameters: the table and the index. How do I find the index of the object if they are all the same? Thanks!

Also, sorry that the code didn’t format correctly.

It might be easier to just use a dictionary, and to store each ‘Minion’ object as the key itself, pointing to itself as the value.

An example of how that’d look:

local minions = {}
for i = 1, scorchSpawnLimit do
local new_minion = redScorchMinion:Clone()
minions[new_minion] = new_minion --//The key/index of the table will the object directly. It's value will also be the object.

minions[new_minion]:Destroy() --//Index the key in the dictionary by using the object itself, then destroy it

I can clarify more if needed, hope this helps. :slight_smile:

1 Like