Chaining Lightning to Nearby Enemies

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.

Hello again! :wave:
I recommend using a function for this than repeating it each time.

local function getNearbyTargets(target)

local nearbyTargets = {}
		for i, mob in ipairs(mobs:GetChildren()) do
			if (mob.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= 10 then
				if mob ~= target and mob:FindFirstChild(“Shocked”).Value == false then
					table.insert(nearbyTargets, mob)
				end
			end
		end

local model = lightningModule.CreateLightning(ball.Position, target.HumanoidRootPart.Position, 10) 
			game.Debris:AddItem(model, .1)
			target:FindFirstChild("Shocked").Value = true
			

	return nearbyTargets
end
local function shockTargets(nearbyTargets, target)

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)
mob:FindFirstChild(“Shocked”).Value = true

shockTargets(getNearbyTargets(target), target)	
			end

end


shockTargets(getNearbyTargets(mob), mob)	

I think this should work but it probably doesn’t. Also sorry I’m on mobile.
If it doesn’t work, organize the code into these two functions and just have it check when getting nearby targets that they aren’t shocked, then repeat the functions but with the affected part as the target, as shown in the messy code above.

Hope this helps!

1 Like