Problems with UI Object Dragging System

So I made a two different ways of dragging different UI elements but the problem is that when I put the GUI Object in a frame, it messes up where the frame is relative to the mouse

Why is this happening? Do I have to take in account the Absolute Size of the frame that the GUI Object is inside of when setting the position of the frame relative to the mouse position?

Code for moving frame ;

frame.Position = UDim2.new(0,mouse.X,0,mouse.Y)

UI Object not in frame ;

UI Obect inside of frame ;

You have to change the RelativeXY property on the thing your dragging or the frame i think

if frame.Parent:IsA("GuiObject") then
    frame.Position = UDim2.new(0, mouse.X-frame.Parent.AbsolutePosition.X, 0, mouse.Y-frame.Parent.AbsolutePosition.Y)
else
    frame.Position = UDim2.new(0,mouse.X,0,mouse.Y)
end

Could you include an explanation of what that snippet of code is doing please

If the frame is inside e.g. a ScreenGui, it uses your working code, since you say it works that way.

If the frame is inside a frame (or any other gui object), it offsets itself by that object’s position (the draggable object’s position is relative to the position of the parent frame while the mouse position isn’t).

2 Likes

Alright thanks for the explanation let me try this out

@emojipasta It worked thanks for the help man, been stuck on this for a day or two

Just use the code below and replace the parts correspondingly

DraggableObject.Position = MousePosition.Positon - ParentFrame.AbsolutePosition