Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- How to use:
- Do Draggable([Frame or Button], [Movement Amount])
- Instead of [Frame or Button] put a Frame or Button (Example: script.Parent.Frame)
- Instead of [Movement Amount] do a percentage of movement you want
- 1 = Full Movement
- 0.5 = Slightly Smooth Movement
- 0.25 = A bit more Smooth Movement
- 0.05 = Smooth Movement
- ]]
- local plr = game.Players.LocalPlayer
- local m = plr:GetMouse()
- local function Lerp2D(x, xend, percentage)
- return x + (xend - x) * percentage
- end
- local function Draggable(frame,movement)
- if frame:IsA("TextButton") then
- local Clicked = false
- local mx = 0
- local my = 0
- local RealPosition = Vector2.new(frame.AbsolutePosition.X,frame.AbsolutePosition.Y)
- frame.MouseButton1Down:Connect(function()
- Clicked = true
- mx = m.X
- my = m.Y
- end)
- m.Button1Up:Connect(function()
- Clicked = false
- end)
- frame.MouseButton1Up:Connect(function()
- Clicked = false
- end)
- m.Move:Connect(function()
- if Clicked then
- RealPosition = Vector2.new(RealPosition.X - (mx - m.X),RealPosition.Y - (my - m.Y))
- mx = m.X
- my = m.Y
- end
- end)
- game:GetService("RunService").RenderStepped:Connect(function()
- local X2 = Lerp2D(frame.Position.X.Offset, RealPosition.X, movement)
- local Y2 = Lerp2D(frame.Position.Y.Offset, RealPosition.Y, movement)
- frame.Position = UDim2.new(0,frame.AbsolutePosition.X - X2,0,frame.AbsolutePosition.Y - Y2)
- end)
- else
- local Clicked = false
- local mx = 0
- local my = 0
- local RealPosition = Vector2.new(frame.AbsolutePosition.X,frame.AbsolutePosition.Y)
- m.Button1Down:Connect(function()
- 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
- Clicked = true
- mx = m.X
- my = m.Y
- end
- end)
- m.Button1Up:Connect(function()
- Clicked = false
- end)
- m.Move:Connect(function()
- if Clicked then
- RealPosition = Vector2.new(RealPosition.X - (mx - m.X),RealPosition.Y - (my - m.Y))
- mx = m.X
- my = m.Y
- end
- end)
- game:GetService("RunService").RenderStepped:Connect(function()
- local X2 = Lerp2D(frame.Position.X.Offset, RealPosition.X, movement)
- local Y2 = Lerp2D(frame.Position.Y.Offset, RealPosition.Y, movement)
- frame.Position = UDim2.new(0,frame.AbsolutePosition.X - X2,0,frame.AbsolutePosition.Y - Y2)
- end)
- end
- end
- --Example: Draggable(script.Parent,0.05)
Add Comment
Please, Sign In to add comment