Advertisement
DrawingJhon

Draggable

Apr 3rd, 2022 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.22 KB | None | 0 0
  1. local player = game:GetService("Players").LocalPlayer
  2. local mouse = player:GetMouse()
  3. local tweenEffect = false
  4.  
  5. local function makeDrag(frame, focusObj)
  6.     if focusObj == nil then
  7.         focusObj = frame
  8.     end
  9.    
  10.     local mouseButton1, Touch = Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch
  11.     local pressing = false
  12.  
  13.     local con1
  14.     local con2
  15.     con1 = focusObj.InputBegan:connect(function(input)
  16.         pressing = true
  17.         local lastPosition
  18.         local dis = UDim2.new(0, mouse.X, 0, mouse.Y) - frame.Position
  19.         local loop
  20.         if input.UserInputType == mouseButton1 or input.UserInputType == Touch then
  21.             loop = game:GetService("RunService").RenderStepped:Connect(function()
  22.                 if pressing == false then loop:Disconnect() return end
  23.                 local pos = UDim2.new(0, mouse.X, 0, mouse.Y) - dis
  24.                 if pos == lastPosition then return else lastPosition = pos end
  25.                 if tweenEffect then
  26.                     frame:TweenPosition(pos, "Out", "Sine", 0.1, true)
  27.                 else
  28.                     frame.Position = pos
  29.                 end
  30.             end)
  31.         end
  32.     end)
  33.     con2 = game:GetService("UserInputService").InputEnded:Connect(function(input)
  34.         if input.UserInputType ~= mouseButton1 and input.UserInputType ~= Touch then return end
  35.         pressing = false
  36.     end)
  37. end
  38.  
  39. makeDrag(script.Parent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement