Issues with rendering position for draggable frame with a parent

I’m attempting to create a draggable frame, that consists of 2 frames (parents), but it doesn’t seem to render as intended and tends to lose the mouse’s position as moved furthermore. I was messing around with the AnchorPoint property, it doesn’t seem to cooperate with the code, alongside the code itself may be malfunctioning. I’ve researched similar issues with this for an hour, tried their solutions, but it kept giving me different bugs. Is there a method to create a smooth, draggable frame?

Concurrent Properties —

  • AnchorPoint is set to 0, 0
  • Drag object is in the frame (mouse listener)
  • The frame is in 2 parents (MainFrame - Holders - DonateHolder)

image

Issue represented in GIF (animation)
https://gyazo.com/29efb4a845fc9720bf330f6cdbc94692

Root of the issue

local donateHeld = false; local dragDonate = donateH.Drag
dragDonate.MouseButton1Down:Connect(function()
	donateHeld = true
end)
mouse.Move:Connect(function()
	if donateHeld then
		donateH.Position = UDim2.new(0, mouse.X, 0, mouse.Y) + (UDim2.fromOffset(mouse.X, mouse.Y) - donateH.Position)
        --malfunction is in the line above.
	end
end)
dragDonate.MouseButton1Up:Connect(function()
	donateHeld = false
end)

— UPDATE —
This issue has been repaired by AljoSven, which can be found throughout the marked solution. The final product is https://gyazo.com/e26f6dc28b03a254c0e3a1a494e652e.

This could be an issue with having multiple parents.

Try setting the position to something like this:

local offset = donateH.Parent.AbsolutePosition
donateH.Position = UDim2.fromOffset(mouse.X - offset.X, mouse.Y - offset.Y) 

Keep in mind you will also have to account for the AnchorPoint of the frame as well as the Topbar offset.

Try this and let me know if it works any better.

The script works better, however, the downside of it is that the cursor only targets the specific position, rather than moving it to its original position. Is there anything that I’ve missed, or would be extra?
https://gyazo.com/fe605b08d2f498c8785b82f4a17fa63f

The AnchorPoint, for the frame, determines the exact position the cursor should target to. I’m unsure about the TextButton though.

You would need to cache the cursor offset when you click the frame and subtract it from your frame position.

    local donateHeld = false; local dragDonate = donateH.Drag
    local cursorOffset = Vector2.new()
    dragDonate.MouseButton1Down:Connect(function()
    	donateHeld = true
        cursorOffset = Vector2.new(mouse.X, mouse.Y) - donateH.AbsolutePosition
    end)
    mouse.Move:Connect(function()
    	if donateHeld then
    		local offset = donateH.Parent.AbsolutePosition + cursorOffset
            donateH.Position = UDim2.fromOffset(mouse.X - offset.X, mouse.Y - offset.Y)
    	end
    end)
    dragDonate.MouseButton1Up:Connect(function()
    	donateHeld = false
        cursorOffset = Vector2.new()
    end)
1 Like

Of course! I was thinking of that idea but didn’t acknowledge where to start - this is one of the best solutions, thank you for the generosity! I’ll update the post and mark your solution.

1 Like

Awesome! I’m glad I could be of help to you.