Attacking Chaining to nearby enemies

image

I have multiple abilities in my game that I want to hit its first target, then chain down a line of enemies that are nearby.

I am able to get the position of all enemies, and check if they are in a certain radius, then chain to ONE nearby enemy. After that I can’t figure out how keep the attacking flowing down the line.

Usually it will just bounce back and forth between the first and second enemy.

make a list of zombies that are in the range, then hit the nearest zombie that is in the list and remove this zombie from the list

1 Like

So I tried this:

Summary
       local arrowTargets = {}
		
		for i, v in ipairs(workspace.MobRealm:GetChildren()) do
			if v ~= target then
				table.insert(arrowTargets, v)
			end
		end
		
		local currentPos = newPosition
		
		local function NearbyTargs()
			for i, v in ipairs(arrowTargets) do
				if v then
					local dist = (v.HumanoidRootPart.Position - currentPos).Magnitude
					if dist <= 10 then
                       print(v, dist, "WITHIN RANGE")
						table.remove(arrowTargets, arrowTargets[v])
						local arrowClone = arrow:Clone()
						arrowClone.Parent = workspace
						arrowClone.Position = currentPos
						
						local rotatedCFrame = CFrame.new(arrowClone.Position, v.HumanoidRootPart.Position)*CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))
						arrowClone.CFrame = rotatedCFrame
						
						local vPosition = v.HumanoidRootPart.Position
						local tween	= TweenService:Create(arrowClone, info, {Position = vPosition})		
						
						tween:Play()
						target.Humanoid:TakeDamage(calcDamage.Value)
						target:FindFirstChild("Burn").Value = true
						game.Debris:AddItem(arrowClone, .3)
						
						currentPos = vPosition
						NearbyTargs()
					end
				end
			end
		end
		
		NearbyTargs()

This only sends the attack from the first target to the second, and does not keep traveling down the line.

I figured calling the function within itself would allow this happen, as I figured it would keep running until the table was empty, but it doesn’t seem to work

EDIT:
I added a print within NearbyTargs which
print("V POS:",v.HumanoidRootPart.Position,"CURRENT POS:",currentPos,"DIST:",dist)

Result:

I’m not sure of what is causing this but there is always x7+ of the same positions all at once.