Having trouble with dragging feature for frame

Hello developers! I’m trying to create a frame that can be dragged around the screen using the mouse, however, the position is always slightly off, and I don’t know why.

Expected result: The frame follows the mouse seamlessly.
Actual result: The frame weirdly changes position for no apparent reason.

Notice how the window goes slightly up and to the right:
https://gyazo.com/0074521b74def4598c7a16dee9a52545
Code:

local function ChangeWindowPosition(Position,AnchorPoint)
	Window.Position = Position
	Window.AnchorPoint = AnchorPoint
end
Window.Topbar.DragArea.MouseButton1Down:Connect(function()
	local StartingX = Window.AbsolutePosition.X*Window.AnchorPoint.X
	local StartingY = Window.AbsolutePosition.Y*Window.AnchorPoint.Y
	print(Mouse.X-StartingX)
	print(Mouse.Y-StartingY)
	StartingX = StartingX/Window.AbsoluteSize.X
	StartingY = StartingY/Window.AbsoluteSize.Y
	print(StartingX)
	print(StartingY)
	ChangeWindowPosition(UDim2.new(0,Mouse.X,0,Mouse.Y), Vector2.new(StartingX,StartingY))
	HoldWindow = Mouse.Move:Connect(function() ChangeWindowPosition(UDim2.new(0,Mouse.X,0,Mouse.Y), Vector2.new(StartingX,StartingY)) end)

end)
Mouse.Button1Up:Connect(function()
	if HoldWindow ~= nil then
		HoldWindow:Disconnect()
		HoldWindow = nil
	end
end)
--This is not the complete script, only includes code related to this issue.
--Sorry for the messy code, I only clean it up once I'm done with it.

Thanks in advance!

Will you be changing the anchor point from (0, 0)? Because if you are not (and I can’t see why you would), then your script becomes much easier to work with.

1 Like

The anchorpoint starts at (0.5,0.5), and is changed by the script in ChangeWindowPosition()

I’ve been able to fix the issue! Turns out AbsolutePosition is not affected by the AnchorPoint, and the mouse object doesn’t ignore gui inset.

local function ChangeWindowPosition(Position,AnchorPoint)
	Window.Position = UDim2.new(0, Position.X, 0, Position.Y) --Creating the UDim2 in the function itself saves up space
	Window.AnchorPoint = AnchorPoint
end
Window.Topbar.DragArea.MouseButton1Down:Connect(function()
	--AbsolutePosition ignores the AnchorPoint
	local StartingX = Window.AbsolutePosition.X 
	local StartingY = Window.AbsolutePosition.Y
	--Get where the mouse is relative to the window
	StartingX = (Mouse.X-StartingX)/Window.AbsoluteSize.X
	StartingY = (Mouse.Y-StartingY)/Window.AbsoluteSize.Y
	HoldWindow = Mouse.Move:Connect(function()ChangeWindowPosition(Vector2.new(Mouse.X,Mouse.Y+36),Vector2.new(StartingX,StartingY)) end)
end)
Mouse.Button1Up:Connect(function()
	if HoldWindow ~= nil then
		HoldWindow:Disconnect()
		HoldWindow = nil
	end
end)
ChangeWindowPosition(Vector2.new(WindowsGui.AbsoluteSize.X/2,WindowsGui.AbsoluteSize.Y/2),Vector2.new(0.5,0.5)) --Set window to starting position
1 Like

the most simple and shortest code I found is frame.Draggable = true but i see code is always nice. I want some tutorial on this although I already understand it.

Are we gonna see some update on this? I wanna see more of this content

1 Like