Alright, so basically, I’ve seen this “snippet” run around for dragging gui’s.
The code was rather inefficient (No offense to the creator), so I decided to simplify it.
This snippet works much better and takes up fewer lines.
local DraggableUI = script.Parent -- Ur frame here
local Dragging = false
local DragInput
local DragStart
local StartPosition
DraggableUI.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
Dragging = true
DragStart = Input.Position
StartPosition = DraggableUI.Position
end
end)
DraggableUI.InputEnded:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
Dragging = false
end
end)
UserInputService.InputChanged:Connect(function(Input)
if Input == DragInput and Dragging then
local Updated = Input.Position - DragStart
DraggableUI.Position = UDim2.new(StartPosition.X.Scale, StartPosition.X.Offset + Updated.X, StartPosition.Y.Scale, StartPosition.Y.Offset + Updated.Y)
elseif Input.UserInputType == Enum.UserInputType.MouseMovement or Input.UserInputType == Enum.UserInputType.Touch then
DragInput = Input
end
end)
If I’m not mistaken, wouldn’t this store the updated position using offset?
This can be a problem on computers/laptops as when you resize your window, the UIs updated position would appear different from where u last left it originally. Maybe you should consider converting the final position to scale?
Yes, this is currently to provide an alternative to the previous snippet that was rather inefficient, I will be creating a full blown script for this soon