Draggable GUI elements stacking

When one or more draggable gui elements are on top of each other, when dragged, they are both moved. This causes an issue because things could become stuck to each other. The drag script im using was from a forum post i dont recall where i got it from. How can I fix this?

local UserInputService = game:GetService("UserInputService")

local gui = script.Parent.dragframe

local dragging
local dragInput
local dragStart
local startPos

local function update(input)
	local delta = input.Position - dragStart
	gui.Parent.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 or input.UserInputType == Enum.UserInputType.Touch then
		dragging = true
		dragStart = input.Position
		startPos = gui.Parent.Position

		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				dragging = false
			end
		end)
	end
end)

gui.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		dragInput = input
	end
end)

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

Don’t use a drag script, use UIdragdetector, though it is studio beta features

To fix script I suggest use a different one or put in a varaible that limits how many items can be dragged and drag the one with the higher zindex

2 Likes

i’ll switch to uidragdetector whenever it leaves beta, ill try this out tomorrow

I think I fixed this in one of my games but I’m not able to remember how, and unfortunately don’t have access to my computer now. If you still want help to this issue, I can remind myself later to enter Studio and try to fix it?

Why script your own dragging when a perfectly working object called UIDragDetector exists?

You need to do some steps to use it though. Link to it here