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.
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)