Lightning Chaining

I have a lightning tower in my game that currently shoots lightning at 1 target, then chains to 1 nearby target.

What I want is for the lightning to chain from 1 mob to another, down a line as long as there a new mob that hasn’t already been shocked within range.

This is the lightning module which creates the lightning:

Summary

local module = {}

function draw(p1, p2, Parent)
local dist = (p2.Position - p1.Position).Magnitude
local part = Instance.new(“Part”, Parent)
part.CanCollide = false
part.Anchored = true
part.Position = p1.Position
part.Material = “Neon”
part.BrickColor = BrickColor.new(“New Yeller”)

part.Size = Vector3.new(.5,.5,dist)
part.CFrame = CFrame.new(p1.Position, p2.Position) * CFrame.new(0,0,-dist/2)

end

module.CreateLightning = function(startPosition, endPosition, numberOfPoints)
local model = Instance.new(“Model”, game.Workspace)
local points = {}

for i = 0, numberOfPoints do
	local offset = Vector3.new(math.random(-5,5), math.random(-2,2), math.random(-5,5))

	if i == 0 or i == numberOfPoints then
		offset = Vector3.new(0,0,0)
	end

	local part = Instance.new("Part", model)
	part.Anchored = true
	part.CanCollide = false
	part.Size = Vector3.new(.5, .5, .5)
	part.Material = "Neon"
	part.BrickColor = BrickColor.new("New Yeller")
	part.Name = 'Point'

	part.Position = startPosition + (endPosition - startPosition).Unit * i * (endPosition - startPosition).Magnitude/numberOfPoints + offset
	points[#points+1] = part
end

for i = 1, #points do
	if points[i+1] ~= nil then
		draw(points[i], points[i+1], model)  -- Note the change here from points[i-1] to points[i+1]
	end
end

return model

end

return module

This is the full lightning tower script which handles targeting and damage:

Summary
local tower = script.Parent.Position
local mobs = workspace.MobRealm
local ball = tower.Parent.Ball
local ServerScriptService = game:GetService("ServerScriptService")
local lightningModule = require(ServerScriptService:WaitForChild("Lightning"))

local Debounce = false

-- BASE TOWER STATS
local cooldown = tower.Parent.BaseSpeed.Value
local damage = tower.Parent.BaseDamage.Value
local insta = tower.Parent.BaseInsta.Value

local function FindNearestTarget()
	local maxDistance = 70
	local nearestTarget = nil

	for i, target in ipairs(mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - tower.Position).Magnitude
		if distance < maxDistance then
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget
end

local adjCooldown

while true do
	local target = FindNearestTarget()
	if target and not Debounce then
		Debounce = true
		
		local speedIncrease = tower.Parent.SpeedIncrease.Value
		local damageIncrease = tower.Parent.DamageIncrease.Value
		local instaIncrease = insta + tower.Parent.InstaIncrease.Value

		local ballTween = game.TweenService:Create(ball, TweenInfo.new(1.8-(1.8*speedIncrease)), {
			Transparency = 0,
			Color = Color3.fromRGB(255, 170, 0)
		})

		ballTween:Play()
		task.wait(1.8-(1.8*speedIncrease))

		if target.Destroying then
			target = FindNearestTarget()
		end
		
		local nearbyTargets = {}
		for i, mob in ipairs(mobs:GetChildren()) do
			if target and mob then
				if (mob.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= 10 then
					if mob ~= target then
						table.insert(nearbyTargets, mob)
					end
				end
			end
		end
		
		if target then
			local model = lightningModule.CreateLightning(ball.Position, target.HumanoidRootPart.Position, 10) 
			game.Debris:AddItem(model, .1)
			
			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)
				
				if math.random(1, 100) <= instaIncrease then
					mob.Humanoid:TakeDamage(target.Humanoid.MaxHealth)
				end
				
				for k in pairs (nearbyTargets) do
					table [k] = nil
				end
				
			end
		end

		
		if target then
			local targethum = target:WaitForChild("Humanoid") or nil
			targethum.Health = math.max(targethum.Health - (damage+(damage*damageIncrease)), 0)
			
			if math.random(1, 100) <= instaIncrease then
				target.Humanoid:TakeDamage(target.Humanoid.MaxHealth)
			end
			
		end
		

		local ballTween2 = game.TweenService:Create(ball, TweenInfo.new(0.2-(0.2*speedIncrease)), {
			Transparency = 0,
			Color = Color3.fromRGB(255, 0, 0)
		})

		ballTween2:Play()
		task.wait(.2-(0.2*speedIncrease))

		Debounce = false
	else
		task.wait(0.1)
	end
end

As for the specific code that ties them together:

local nearbyTargets = {}
		for i, mob in ipairs(mobs:GetChildren()) do
			if target and mob then
				if (mob.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude <= 10 then
					if mob ~= target then
						table.insert(nearbyTargets, mob)
					end
				end
			end
		end
		
		if target then
			local model = lightningModule.CreateLightning(ball.Position, target.HumanoidRootPart.Position, 10) 
			game.Debris:AddItem(model, .1)
			
			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)
				
				if math.random(1, 100) <= instaIncrease then
					mob.Humanoid:TakeDamage(target.Humanoid.MaxHealth)
				end
				
				for k in pairs (nearbyTargets) do
					table [k] = nil
				end
				
			end
		end

Basically, I can’t figure out how to find nearby target, that haven’t already been targeted.