I want to achieve a lightning effect that can chain down a line of enemies.
I currently have this script; which chains to 1 nearby enemy:
Summary
local nearbyTargets = {}
for i, mob in ipairs(mobs:GetChildren()) do
if (mob.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= 10 then
if mob ~= target then
table.insert(nearbyTargets, mob)
end
end
end
if target then
local model = lightningModule.CreateLightning(ball.Position, target.HumanoidRootPart.Position, 10)
game.Debris:AddItem(model, .1)
target:FindFirstChild("Shocked").Value = true
for i, mob in pairs(nearbyTargets) do
local nearbyTargets = {}
local model = lightningModule.CreateLightning(target.HumanoidRootPart.Position, mob.HumanoidRootPart.Position, 3)
game.Debris:AddItem(model, .1)
mob:FindFirstChild("Humanoid").Health = math.max(mob:FindFirstChild("Humanoid").Health - (damage+(damage*damageIncrease)), 0)
for k in pairs (nearbyTargets) do
table [k] = nil
end
end
end
I cannot seem to figure out how to chain from the 2nd Mob to the 3rd Mob.
I’ve tried repeating the original nearbyTargets table, but it targets the original target.
To fix that I tried adding and checking for a Shocked value, which didn’t seem to help at all.
I’m completely lost on finding the next target, and can’t seem to find a topic similar to this.
EDIT:
This is currently how the lightning chains, to all targets within 10 studs.
When it targets the front Mob, I want it to chain to the 2nd, then to the 3rd.

