How would you accurately set a GUI object's absolute position?

Hello! With my attempts at making a drag and drop GUI frame, I’ve had some issues, primarily this one.

My goal is to have a drag and drop frame, but while it works inside of static objects (Like a normal frame) it completely breaks inside of a scrolling frame.

Example:

It seems like when I scroll the GUI the positions become horribly offset, completely ruining the ‘drag’ effect.

My code (The function that handles the dragging.)

--// Services
local userInputService = game:GetService("UserInputService")
local guiService = game:GetService("GuiService")
--// Values
local topbarInset = guiService:GetGuiInset()

function drag(frameToDrag)
    local absolutePosition = userInputService:GetMouseLocation()
    local relativePosition = absolutePosition - frameToDrag.Parent.AbsolutePosition

    frameToDrag.Position = UDim2.fromOffset(relativePosition.X, relativePosition.Y - inset.Y)
end

(Some of the code was used from a similar devforum post, but I can’t say which.)

Thank you for any help I recieve! :smile:

6 Likes

Figured it out! I added the canvas’ position on the Y axis and everything is working well again.

Seems like a ‘hacky’ solution to me… but it works!

(Posting so anybody who has the same problem can have an easier solution, however unlikely that may be)

11 Likes