Help with connecting two frames together with a line

Hello, I am creating a script that will create a new frame, every frame, where the mouse is located. It works, but I wanted to make it look nicer than it does (its blocky and the frames are far away from each other) by adding lines between each frame point, but I’m having some issues:
Screenshot_22

Here is the code:

local m = game:GetService("Players").LocalPlayer:GetMouse()
local p = game:GetService("Players").LocalPlayer
local sgui = p.PlayerGui:WaitForChild("maingui")
local lines = {}

function drawPath(Line, P1, P2)
	local Size = game.Workspace.CurrentCamera.ViewportSize
	local startX, startY = P1.Position.X.Scale*Size.X, P1.Position.Y.Scale*Size.Y
	local endX, endY = P2.Position.X.Scale*Size.X, P2.Position.Y.Scale*Size.Y
	local startVector = Vector2.new(startX, startY)
	local endVector = Vector2.new(endX, endY)
	local Distance = (startVector - endVector).Magnitude
	Line.AnchorPoint = Vector2.new(0.5, 0.5)
	Line.Size = UDim2.new(0, Distance, 0, 5)
	Line.Position = UDim2.new(0, (startX + endX) / 2, 0, (startY + endY) / 2)
	Line.Rotation = math.atan2(endY - startY, endX - startX) * (180 / math.pi)
end

local isholding = false
m.Button2Down:Connect(function()
	for index,ui in pairs(sgui:GetChildren()) do
		if ui:IsA("GuiObject") then
			ui:Destroy()
		end
	end
end)

m.Button1Down:Connect(function()
	isholding = true
end)

m.Button1Up:Connect(function()
	isholding = false
end)

spawn(function()
	game:GetService("RunService").RenderStepped:Connect(function()
		if isholding then
			if #lines > 0 then
				local ui = Instance.new("Frame",sgui)
				ui.BackgroundTransparency = 0
				ui.Size = UDim2.new(0,5,0,5)
				ui.Position = UDim2.new(0,m.X,0,m.Y)
				ui.AnchorPoint = Vector2.new(0.5,0.5)
				table.insert(lines,ui)
				local Line = Instance.new("Frame",sgui)
				local current_udim = ui
				local last_udim = lines[tonumber(table.find(lines,ui))-1]
				drawPath(Line,current_udim,last_udim)
			else
				local ui = Instance.new("Frame",sgui)
				ui.BackgroundTransparency = 0
				ui.Size = UDim2.new(0,5,0,5)
				ui.Position = UDim2.new(0,m.X,0,m.Y)
				ui.AnchorPoint = Vector2.new(0.5,0.5)
				table.insert(lines,ui)
			end
		end
	end)
end)

There are no errors in the output. Any help would be appreciated :slight_smile: