Drawing line from mouse to UI object does not work

I’m using code from here

For 1 UI frame to another

game:GetService("RunService").RenderStepped:Connect(function()
	local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
	
	drawPath(
		Gui.Spot1.Position,
		Gui.Spot2.Position
	)
end)

From UI object to mouse. It stays in the top left of screen, no matter where my mouse is on the screen

game:GetService("RunService").RenderStepped:Connect(function()
	local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
	
	drawPath(
		UDim2.new(0, MouseLocation.X, 0, MouseLocation.Y),
		Gui.Spot2.Position
	)
end)

Unless you edited it, it looks like inputs are scale based. So you’re putting the mouse in the offset, but it draws between scale positions (so mouse is effectively 0,0). You’d have to change the code to use absolute position instead of scale for drawing, or you’d have to convert the mouse position to scale I think.

Even when I convert to scale tho

local startX, startY = P1.X.Offset/Size.X, P1.Y.Offset/Size.Y

it still does same thing

Have you printed that to ensure you’re getting the values you think you are? Because to me that seems like it should work.

Actually I just realize you’re converting it to scale there when the original code converts it to offset. You should just add your offset to the already calculated value instead. That will make it work for both offset and scale.

local startX, startY = P1.X.Scale*Size.X + P1.X.Offset, P1.Y.Scale*Size.Y + P1.Y.Offset

What I did should work tho. From the original code to my code

I managed to fix, but it’s way way off
Red box is where my mouse is, line is no where near the mouse

I just edited my previous response to include the fixed line.

Sorry I was a bit slow coming to my solution. Try using the original code, but replace the line in my previous response with what I posted.

Line is still way off from the mouse :confused:

Can you print the mouse position and startX startY to make sure they’re the same?

And while you are at it can you also print endX endY and the absolute position of the gui? These also should be the same if I’m correct.

Now that I can sit down at my computer this is what I got. Of course for some reason I left gui inset on and just coded around it.

local Gui = script.Parent

local Lines = Instance.new("Folder")
Lines.Parent = Gui

local guiInset = game:GetService("GuiService"):GetGuiInset()
function drawPath(P1, P2)
	local Line = Instance.new("Frame")
	
	local Size = workspace.CurrentCamera.ViewportSize - guiInset
	
	local startPoint = Vector2.new(P1.X.Scale, P1.Y.Scale)*Size + Vector2.new(P1.X.Offset, P1.Y.Offset)
	local endPoint = Vector2.new(P2.X.Scale, P2.Y.Scale)*Size + Vector2.new(P2.X.Offset, P2.Y.Offset)

	local magnitude = (startPoint - endPoint).Magnitude
	local center = (startPoint + endPoint) / 2
	
	Line.Size = UDim2.new(0, magnitude, 0, 5)
	Line.Position = UDim2.fromOffset(center.X, center.Y)
	Line.Rotation = math.atan2(endPoint.Y - startPoint.Y, endPoint.X - startPoint.X) * (180 / math.pi)
	
	Line.AnchorPoint = Vector2.new(0.5, 0.5)
	Line.Parent = Lines
end


game:GetService("RunService").RenderStepped:Connect(function()
	local MouseLocation = game:GetService("UserInputService"):GetMouseLocation() - guiInset --That gui inset is important I think.
	
	Lines:ClearAllChildren() --I'm lazy ok
	drawPath(
		Gui.Spot1.Position,
		Gui.Spot2.Position
	)
	
	drawPath(
		Gui.Spot1.Position,
		UDim2.fromOffset(MouseLocation.X, MouseLocation.Y)
	)
end)

Sorry for bumping this topic, but it’s not marked as solved.
A very common rookie mistake I’ve made is using the position of the UI. This may cause offsets such as if your UI is in another object. This is why the read only property AbsolutePosition exists. It basically tells you the absolute amount of pixels an object is from the top left which should have the same origin as the position returned from the mouse, causing no inaccurate math.