Problem with Generating Part Crack Effect

I’m trying to create a part generated crack effect, but my lines don’t follow each other.


Here’s the code:

local function crackTrail()
		local nerve = tab.CF * CFrame.Angles(0,math.rad(math.random(0,360)),0)
		
		local startPos, endPos = nerve.p, nerve * CFrame.new(0,0,-50).Position
		local maxParts = math.random(5, 10)
		local dist = (startPos - endPos).Magnitude
		
		local p = Instance.new("Part")
		p.Name= "Start"
		p.Material = Enum.Material.SmoothPlastic
		p.Color = Color3.fromRGB(255,0,0)
		p.Size = Vector3.new(1,1,1)
		p.CFrame = CFrame.new(startPos)
		p.Parent = linesFol
		
		local p = Instance.new("Part")
		p.Name= "End"
		p.Material = Enum.Material.SmoothPlastic
		p.Color = Color3.fromRGB(0,255,0)
		p.Size = Vector3.new(1,1,1)
		p.CFrame = CFrame.new(endPos)
		p.Parent = linesFol
		
		startPos = CFrame.new(startPos, endPos) * CFrame.new(0,0,-dist/maxParts / 2).Position
		
		for i = 1, maxParts do
			local RNG = Random.new()

			local p = Instance.new("Part")
			p.Material = Enum.Material.SmoothPlastic
			p.Size = Vector3.new(1,1,1)
			p.CFrame = CFrame.new(startPos)
			p.Parent = linesFol
			
			local offset = Vector3.new(math.random(-10, 10), 0, 0)
			
			local template = fx.CrackLine:Clone()
			template.CFrame = CFrame.lookAt(startPos, endPos + offset)
			template.Size = Vector3.new(template.Size.X, template.Size.Y, dist / maxParts)
			template.Parent = linesFol
			
			startPos = CFrame.new(template.CFrame.Position, endPos) * CFrame.new(0,0,-dist/maxParts).Position
			task.wait()
		end
	end	

I made something like this a while ago, not sure if you would want it but here’s the place file: (press f to generate a crack trail)
Trail Creator.rbxl (34.5 KB)