Need help with UI Dragging system

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? When the UI is clicked, it should follow the mouse until the player clicks again.

  2. What is the issue?

  3. What solutions have you tried so far? I have tried making it a separate function, I have tried subtracting the Absolute Position.

This is the only part of the script that isn’t working.

local UserInputService=game:GetService('UserInputService');
local gui=script.Parent;

local dragging,dragInput,dragStart,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 or input.UserInputType==Enum.UserInputType.Touch then
		dragging,dragStart,startPos=true,input.Position,gui.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)

I’m not sure if you are just trying this as a fun experiment, or if you just need a draggable GUI element.

If the answer is the latter, please refer to this module for simple and intuitive UI dragging:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.