I’m using code from here
So heres your issue, you didn’t actually convert the starting positions back into offset values, and VineyardVine was using the exact opposite of that, however he was right.
function drawPath(Line, P1, P2)
local Size = 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, en…
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)
tlr22
(FusionOak)
March 11, 2022, 2:31pm
#2
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
tlr22
(FusionOak)
March 11, 2022, 2:36pm
#4
Have you printed that to ensure you’re getting the values you think you are? Because to me that seems like it should work.
tlr22
(FusionOak)
March 11, 2022, 2:44pm
#6
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
tlr22
(FusionOak)
March 11, 2022, 2:50pm
#9
I just edited my previous response to include the fixed line.
tlr22
(FusionOak)
March 11, 2022, 2:53pm
#10
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
tlr22
(FusionOak)
March 11, 2022, 2:59pm
#12
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.
tlr22
(FusionOak)
March 12, 2022, 3:15am
#14
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)
tomsterBG
(tomsterBG)
April 16, 2023, 3:33pm
#15
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.