Creating a part trail problem

So I’m trying to make a touchable trail(similar to the Hoverboard.io minigame in RB Battles). And I found an old post:
Touched Event for Trails - #2 by fret13103
Here’s my script:

local SegmentVerts = {}
local inZone = script.Parent.PlayerZone.InZone

function JoinVerts(Pos1, Pos2)
	local Length = (Pos2 - Pos1).Magnitude
	local part = Instance.new("Part")
	part.Parent = workspace.FlashFlame_Roblox.HumanoidRootPart
	part.Material = Enum.Material.SmoothPlastic
	part.CanCollide = false
	part.Anchored = true
	part.Size = Vector3.new(2, 0.5, Length)
	part.CFrame = CFrame.new(Pos1, Pos2)
end

while true do
	coroutine.resume(coroutine.create(
		function()
			for i = 1, 6 do
				if (workspace.FlashFlame_Roblox.Humanoid.MoveDirection.Magnitude > 0) and (inZone.Value == false) then
					wait(0.5)
					SegmentVerts[i] = workspace.FlashFlame_Roblox.HumanoidRootPart.Position
					if #SegmentVerts > 1 then
						JoinVerts(SegmentVerts[i - 1], SegmentVerts[i])
					end
				end
				
				if inZone.Value == true then
					for i, v in pairs(workspace.FlashFlame_Roblox.HumanoidRootPart:GetChildren()) do
						if v:IsA("Part") then
							v:Destroy()
						end
					end
				end
			end
		end
	))
	wait()
end

The problem is that sometimes Pos2 will be nil. Also, even when it works, it will be very laggy because the parts are short and a ton of it is being created. I can’t think of a way to lengthen the parts but also lower the amount of parts to go smooth like the minigame.

Hope this helps your question since there are already similar topics to use for this.


I’ve already known this.

Figured it out.