How to drag UI with right mouse?

Hi i there are any way to drag UI by UI drag detector with right mouse ? Thx. (without script if have solution)

1 Like

You’ll need to use a script if you want to do that, since UIDragDetectors don’t currently support dragging GUIs with the right mouse button


@hieuhuyroblox

This script should accomplish your goal:

-- Needs to be a LocalScript that's a direct child of the GUI you want to drag, in-order to work correctly!
local GuiService = game:GetService("GuiService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local frame = script.Parent -- I used a Frame for testing, but you should set this to the GUI that you'd like to drag


local active = false -- A debounce that will prevent registering input if the drag is already active


local function onHeartbeat()
	local mousePosition = UserInputService:GetMouseLocation() - GuiService:GetGuiInset()

	frame.Position = UDim2.fromOffset(mousePosition.X, mousePosition.Y)
end

local function onInputBegan(inputObject: InputObject)
	if active then return end

	if inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
		active = true

		local connection1 = RunService.Heartbeat:Connect(onHeartbeat)

		local connection2 -- This is done so that the drag is cancelled even if they let go of the right mouse button while it happens to be outside of the GUI
		connection2 = UserInputService.InputEnded:Connect(function(inputObject: InputObject)
			if active and inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
				connection1:Disconnect()
				connection2:Disconnect()
				active = false
			end
		end)
	end
end

frame.InputBegan:Connect(onInputBegan)
3 Likes