Scriptorz5

better dragging

Sep 13th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. local UserInputService = game:GetService("UserInputService")
  2.  
  3. local gui = script.Parent
  4.  
  5. local dragging
  6. local dragInput
  7. local dragStart
  8. local startPos
  9.  
  10. local function update(input)
  11. local delta = input.Position - dragStart
  12. wait() -- remove this line if you don't want the dragging to be smoother
  13. gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
  14. end
  15.  
  16. gui.InputBegan:Connect(function(input)
  17. if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
  18. dragging = true
  19. dragStart = input.Position
  20. startPos = gui.Position
  21.  
  22. input.Changed:Connect(function()
  23. if input.UserInputState == Enum.UserInputState.End then
  24. dragging = false
  25. end
  26. end)
  27. end
  28. end)
  29.  
  30. gui.InputChanged:Connect(function(input)
  31. if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
  32. dragInput = input
  33. end
  34. end)
  35.  
  36. UserInputService.InputChanged:Connect(function(input)
  37. if input == dragInput and dragging then
  38. update(input)
  39. end
  40. end)
Add Comment
Please, Sign In to add comment