Positioning a GUI To Mouse Position

I’m working on a project and I need to move a GUI to the mouse’s X position. My issue is that GUI positions are relative to their parent object. How would I convert the mouse position to relative GUI position?

2 Likes

For more context, I’m making a video editor and I want you to be able to move the timeline (or whatever it’s called) to your mouse position when clicked.
image

1 Like

Try subtracting the absolute position of the parent frame from the mouse position

frame.Position = mousePos - frame.Parent.AbsolutePosition
2 Likes

Almost works
https://gyazo.com/c601244b4681753004d2ed978c8f45fc

local Mouse = game.Players.LocalPlayer:GetMouse()

game:GetService("RunService").RenderStepped:Connect(function()
	script.Parent.Position = UDim2.new(0, Mouse.X - script.Parent.AbsolutePosition.X, 0, 0)
end)

This Code is for testing purposes, I will update it later

I was able to solve this issue with this code

local UserInputService = game:GetService("UserInputService")

game:GetService("RunService").RenderStepped:Connect(function()
	local MousePosition = UserInputService:GetMouseLocation()
	local ContainerPosition = script.Parent.Parent.AbsolutePosition
	local TracerPosition = script.Parent.AbsolutePosition
	script.Parent.Position = UDim2.new(0, MousePosition.X - ContainerPosition.X, 0, 0)
end)

Hope this helps anyone who also has this problem

1 Like

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