Drawing a line between two TextLabels isn't working

I would like a line to be drawn from one TextLabel to another in my plugin.

I found a function to achieve this and implemented it into my script like so.

local function drawLine(pointA, pointB)
	local distance = math.sqrt(math.pow(pointA.X - pointB.X, 2) + math.pow(pointA.Y - pointB.Y, 2))
	local center = Vector2.new((pointA.X + pointB.X) / 2, (pointA.Y + pointB.Y) / 2)	
	local rotation = math.atan2(pointA.Y - pointB.Y, pointA.X - pointB.X)
	local LINE_THICKNESS = 1

	local line = Instance.new("Frame")
	line.Name = "Remove"
	line.BackgroundColor3 = Color3.new(1, 1, 1)
	line.AnchorPoint = Vector2.new(0.5, 0.5)
	line.Size = UDim2.new(0, distance, 0, LINE_THICKNESS)
	line.Position = UDim2.new(0, center.X, 0, center.Y)
	line.Rotation = math.deg(rotation)
	line.Parent = view
end

And later in my script I am doing…

drawLine(GuiObjectA.AbsolutePosition, GuiObjectB.AbsolutePosition)

However this results in…

I have also tried with the following function instead, which came the closest to what I want.

local function drawLine(pointA, pointB)
	local size = workspace.CurrentCamera.ViewportSize
	local startX, startY = pointA.Position.X.Scale * size.X, pointA.Position.Y.Scale * size.Y
	local endX, endY = pointB.Position.X.Scale * size.X, pointB.Position.Y.Scale * size.Y
	local startVector = Vector2.new(startX, startY)
	local endVector = Vector2.new(endX, endY)
	local distance = (startVector - endVector).Magnitude
	local LINE_THICKNESS = 1

	local line = Instance.new("Frame")
	line.Name = "Remove"
	line.BackgroundColor3 = Color3.new(1, 1, 1)
	line.AnchorPoint = Vector2.new(0.5, 0.5)
	line.Size = UDim2.new(0, distance, 0, LINE_THICKNESS)
	line.Position = UDim2.new(0, (startX + endX) / 2, 0, (startY + endY) / 2)
	line.Rotation = math.atan2(endY - startY, endX - startX) * (180 / math.pi)
	line.Parent = view
end

I don’t really understand the math and I’m not great with Gui. Anyone have any ideas how I can get this to work?

The center calculation is off, since it’s just taking the average of the 2 points relative to the top left of the screen, which is not the center. The correct center calculation is:

local center = (pointB - pointA) / 2 + pointA

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.