Advertisement
_EvilDeveloper

Draggability Function (Roblox GUI Scripting) [For any GUI Object]

Apr 22nd, 2023 (edited)
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | Science | 0 0
  1. -- Made by TheEvilDeveloper
  2.  
  3. local function Draggable(Object: GuiObject, Draggable: boolean)
  4.     local TweenService = game:GetService("TweenService")
  5.     local UserInputService = game:GetService("UserInputService")
  6.  
  7.     local TweenSpeed = 0.15 -- Delay
  8.     local DragSpeed = 0.25 -- Speed
  9.     local DragToggle = nil
  10.     local DragInput = nil
  11.     local DragStart = nil
  12.     local DragPosition = nil
  13.     local StartPosition = nil
  14.  
  15.     local function UpdateInput(Input)
  16.         if not Draggable then return end        
  17.         local Delta = Input.Position - DragStart
  18.         local Location = UDim2.new(StartPosition.X.Scale, StartPosition.X.Offset + Delta.X, StartPosition.Y.Scale, StartPosition.Y.Offset + Delta.Y)
  19.         TweenService:Create(Object, TweenInfo.new(TweenSpeed), {Position = Location}):Play()
  20.     end
  21.  
  22.     Object.InputBegan:Connect(function(Input)
  23.         if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
  24.             DragToggle = true
  25.             DragStart = Input.Position
  26.             StartPosition = Object.Position
  27.             Input.Changed:Connect(function()
  28.                 if Input.UserInputState == Enum.UserInputState.End then
  29.                     DragToggle = false
  30.                 end
  31.             end)
  32.         end
  33.     end)
  34.  
  35.     Object.InputChanged:Connect(function(Input)
  36.         if Input.UserInputType == Enum.UserInputType.MouseMovement or Input.UserInputType == Enum.UserInputType.Touch then
  37.             DragInput = Input
  38.         end
  39.     end)
  40.  
  41.     UserInputService.InputChanged:Connect(function(Input)
  42.         if Input == DragInput and DragToggle then
  43.             UpdateInput(Input)
  44.         end
  45.     end)
  46. end
  47.  
  48. --[[
  49.  
  50. <<EXAMPLE>>
  51.  
  52. local Gui = script.Parent
  53. local Frame = Gui:WaitForChild("DraggableFrame")
  54.  
  55. Draggable(Frame, true) -- Object: GuiObject, Draggable: boolean
  56.  
  57. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement