Mr_3242

Makes any gui draggable (top bar only)

Jun 2nd, 2026
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | Gaming | 0 0
  1. --// Professional Window Drag System
  2.  
  3. local Players = game:GetService("Players")
  4. local UserInputService = game:GetService("UserInputService")
  5.  
  6. local player = Players.LocalPlayer
  7. if not player then return end
  8.  
  9. local playerGui = player:WaitForChild("PlayerGui")
  10.  
  11. local processed = setmetatable({}, {__mode = "k"})
  12. local MIN_SIZE = 140
  13. local TOPBAR_HEIGHT = 50
  14.  
  15. local function isValidFrame(obj)
  16.     if not obj:IsA("Frame") then return false end
  17.     if processed[obj] then return false end
  18.     if not obj.Parent or not obj.Parent:IsA("ScreenGui") then return false end
  19.     if not obj.Visible then return false end
  20.     if obj.AbsoluteSize.X < MIN_SIZE or obj.AbsoluteSize.Y < MIN_SIZE then return false end
  21.     if not obj:IsDescendantOf(playerGui) then return false end
  22.     return true
  23. end
  24.  
  25. local function isInsideTopBar(frame, position)
  26.     local absPos = frame.AbsolutePosition
  27.     local relativeY = position.Y - absPos.Y
  28.    
  29.     return relativeY >= 0 and relativeY <= TOPBAR_HEIGHT
  30. end
  31.  
  32. local function makeDraggable(frame)
  33.     if not isValidFrame(frame) then return end
  34.     processed[frame] = true
  35.    
  36.     local dragging = false
  37.     local activeInput = nil
  38.     local startPos
  39.     local startOffset
  40.    
  41.     frame.InputBegan:Connect(function(input)
  42.         if input.UserInputType ~= Enum.UserInputType.Touch
  43.             and input.UserInputType ~= Enum.UserInputType.MouseButton1 then
  44.             return
  45.         end
  46.        
  47.         if input.UserInputState ~= Enum.UserInputState.Begin then
  48.             return
  49.         end
  50.        
  51.        
  52.         if not isInsideTopBar(frame, input.Position) then
  53.             return
  54.         end
  55.        
  56.         dragging = true
  57.         activeInput = input
  58.         startPos = input.Position
  59.         startOffset = frame.Position
  60.     end)
  61.    
  62.     frame.InputChanged:Connect(function(input)
  63.         if not dragging then return end
  64.         if input ~= activeInput then return end
  65.        
  66.         local delta = input.Position - startPos
  67.        
  68.         frame.Position = UDim2.new(
  69.             startOffset.X.Scale,
  70.             startOffset.X.Offset + delta.X,
  71.             startOffset.Y.Scale,
  72.             startOffset.Y.Offset + delta.Y
  73.         )
  74.     end)
  75.    
  76.     frame.InputEnded:Connect(function(input)
  77.         if input == activeInput then
  78.             dragging = false
  79.             activeInput = nil
  80.         end
  81.     end)
  82. end
  83.  
  84. for _, obj in ipairs(playerGui:GetDescendants()) do
  85.     makeDraggable(obj)
  86. end
  87.  
  88. playerGui.DescendantAdded:Connect(function(obj)
  89.     makeDraggable(obj)
  90. end)
Tags: delta
Add Comment
Please, Sign In to add comment