How would I make my code to use scale to drag gui objects?

Hi, there! I wanted to make some of my UI objects to be able to drag, however, I’ve got a working code, but there’s one issue which is it only drags the object using offset which I don’t want, here’s my code:

local UserInputService = game:GetService("UserInputService")

local gui = script.Parent

local dragging
local dragInput
local dragStart
local startPos

local function update(input)
    local delta = input.Position - dragStart
    gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
end

gui.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        
        dragging = true;
        startPos = gui.Position;
        dragStart = input.Position;
        
        input.Changed:Connect(function()
            if input.UserInputState == Enum.UserInputState.End then
                dragging = false;
            end
        end)
    end
end)

UserInputService.InputChanged:Connect(function(input)
    if dragging and input.UserInputType == Enum.UserInputType.MouseMovement then
        update(input)
    end
end)

Help is appreciated! :love_you_gesture:

I got it working! Thanks to this post:

1 Like