local CoreGui = game:GetService("CoreGui") local UserInputService = game:GetService("UserInputService") local function IsMouseOverFrame(Frame, Position) local AbsPos, AbsSize = Frame.AbsolutePosition, Frame.AbsoluteSize return Position.X >= AbsPos.X and Position.X <= AbsPos.X + AbsSize.X and Position.Y >= AbsPos.Y and Position.Y <= AbsPos.Y + AbsSize.Y end function MakeDraggable(MainFrame, DragFrame) if not MainFrame or not DragFrame then return end local dragging = false local dragStart = nil local startPos = nil local currentTouch = nil local function updatePosition(input) if not dragging or not dragStart then return end local delta = input.Position - dragStart MainFrame.Position = UDim2.new( startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y ) end local function resetDragState() dragging = false dragStart = nil startPos = nil currentTouch = nil end local mouseButton1Connection = nil local touchEndedConnection = nil local movementConnection = nil local function onInputEnded() if dragging then resetDragState() if mouseButton1Connection then mouseButton1Connection:Disconnect() mouseButton1Connection = nil end if touchEndedConnection then touchEndedConnection:Disconnect() touchEndedConnection = nil end if movementConnection then movementConnection:Disconnect() movementConnection = nil end end end local function onInputEndedHandler(endInput) if dragging then if endInput.UserInputType == Enum.UserInputType.MouseButton1 then onInputEnded() elseif endInput.UserInputType == Enum.UserInputType.Touch and endInput == currentTouch then onInputEnded() end end end local function onMovement(input) if dragging and (input.UserInputType == Enum.UserInputType.MouseMovement or input == currentTouch) then updatePosition(input) end end local function onInputBegan(input) local isClick = input.UserInputState == Enum.UserInputState.Begin and (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) if isClick and not dragging then if IsMouseOverFrame(DragFrame, input.Position) then dragging = true dragStart = input.Position startPos = MainFrame.Position if input.UserInputType == Enum.UserInputType.Touch then currentTouch = input else currentTouch = nil end mouseButton1Connection = UserInputService.InputEnded:Connect(function(endInput) if endInput.UserInputType == Enum.UserInputType.MouseButton1 then onInputEndedHandler(endInput) end end) touchEndedConnection = UserInputService.InputEnded:Connect(function(endInput) if endInput.UserInputType == Enum.UserInputType.Touch then onInputEndedHandler(endInput) end end) movementConnection = UserInputService.InputChanged:Connect(function(moveInput) if moveInput.UserInputType == Enum.UserInputType.MouseMovement or moveInput.UserInputType == Enum.UserInputType.Touch then onMovement(moveInput) end end) end end end local inputBeganConnection = DragFrame.InputBegan:Connect(onInputBegan) local cleanupFunction = function() inputBeganConnection:Disconnect() if mouseButton1Connection then mouseButton1Connection:Disconnect() end if touchEndedConnection then touchEndedConnection:Disconnect() end if movementConnection then movementConnection:Disconnect() end end return cleanupFunction end local draggableGui = Instance.new("ScreenGui") draggableGui.Name = "DraggableUI" draggableGui.Parent = CoreGui draggableGui.IgnoreGuiInset = true local mainFrame = Instance.new("Frame") mainFrame.Size = UDim2.new(0, 300, 0, 200) mainFrame.Position = UDim2.new(0.5, -150, 0.5, -100) mainFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30) mainFrame.BackgroundTransparency = 0.8 mainFrame.Active = true mainFrame.Parent = draggableGui local dragHeader = Instance.new("Frame") dragHeader.Size = UDim2.new(1, 0, 0, 40) dragHeader.Position = UDim2.new(0, 0, 0, 0) dragHeader.BackgroundColor3 = Color3.fromRGB(60, 60, 60) dragHeader.BackgroundTransparency = 0.5 dragHeader.Active = true dragHeader.Parent = mainFrame local title = Instance.new("TextLabel") title.Size = UDim2.new(1, 0, 1, 0) title.Position = UDim2.new(0, 0, 0, 0) title.BackgroundTransparency = 1 title.Text = "Draggable Window" title.TextColor3 = Color3.new(1, 1, 1) title.TextScaled = true title.Font = Enum.Font.SourceSansBold title.Parent = dragHeader MakeDraggable(mainFrame, dragHeader)