Plus_Gaming

Enable Draggable 2.0

Nov 4th, 2021 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. --[[
  2. How to use:
  3. Do Draggable([Frame or Button], [Movement Amount])
  4. Instead of [Frame or Button] put a Frame or Button (Example: script.Parent.Frame)
  5. Instead of [Movement Amount] do a percentage of movement you want
  6. 1 = Full Movement
  7. 0.5 = Slightly Smooth Movement
  8. 0.25 = A bit more Smooth Movement
  9. 0.05 = Smooth Movement
  10. ]]
  11.  
  12. local plr = game.Players.LocalPlayer
  13. local m = plr:GetMouse()
  14. local function Lerp2D(x, xend, percentage)
  15.     return x + (xend - x) * percentage
  16. end
  17. local function Draggable(frame,movement)
  18.     if frame:IsA("TextButton") then
  19.         local Clicked = false
  20.         local mx = 0
  21.         local my = 0
  22.         local RealPosition = Vector2.new(frame.AbsolutePosition.X,frame.AbsolutePosition.Y)
  23.         frame.MouseButton1Down:Connect(function()
  24.             Clicked = true
  25.             mx = m.X
  26.             my = m.Y
  27.         end)
  28.         m.Button1Up:Connect(function()
  29.             Clicked = false
  30.         end)
  31.         frame.MouseButton1Up:Connect(function()
  32.             Clicked = false
  33.         end)
  34.         m.Move:Connect(function()
  35.             if Clicked then
  36.                 RealPosition = Vector2.new(RealPosition.X - (mx - m.X),RealPosition.Y - (my - m.Y))
  37.                 mx = m.X
  38.                 my = m.Y
  39.             end
  40.         end)
  41.         game:GetService("RunService").RenderStepped:Connect(function()
  42.             local X2 = Lerp2D(frame.Position.X.Offset, RealPosition.X, movement)
  43.             local Y2 = Lerp2D(frame.Position.Y.Offset, RealPosition.Y, movement)
  44.             frame.Position = UDim2.new(0,frame.AbsolutePosition.X - X2,0,frame.AbsolutePosition.Y - Y2)
  45.         end)
  46.     else
  47.         local Clicked = false
  48.         local mx = 0
  49.         local my = 0
  50.         local RealPosition = Vector2.new(frame.AbsolutePosition.X,frame.AbsolutePosition.Y)
  51.         m.Button1Down:Connect(function()
  52.             if (m.X >= frame.AbsolutePosition.X) and (m.Y >= frame.AbsolutePosition.Y) and (m.X <= (frame.AbsolutePosition.X + frame.AbsoluteSize.X)) and (m.Y <= (frame.AbsolutePosition.Y + frame.AbsoluteSize.Y)) then
  53.                 Clicked = true
  54.                 mx = m.X
  55.                 my = m.Y
  56.             end
  57.         end)
  58.         m.Button1Up:Connect(function()
  59.             Clicked = false
  60.         end)
  61.         m.Move:Connect(function()
  62.             if Clicked then
  63.                 RealPosition = Vector2.new(RealPosition.X - (mx - m.X),RealPosition.Y - (my - m.Y))
  64.                 mx = m.X
  65.                 my = m.Y
  66.             end
  67.         end)
  68.         game:GetService("RunService").RenderStepped:Connect(function()
  69.             local X2 = Lerp2D(frame.Position.X.Offset, RealPosition.X, movement)
  70.             local Y2 = Lerp2D(frame.Position.Y.Offset, RealPosition.Y, movement)
  71.             frame.Position = UDim2.new(0,frame.AbsolutePosition.X - X2,0,frame.AbsolutePosition.Y - Y2)
  72.         end)
  73.     end
  74. end
  75.  
  76. --Example: Draggable(script.Parent,0.05)
Add Comment
Please, Sign In to add comment