Dragging Frames Snippet (Draggable Property Alternative)

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)

Shorter, and better.

5 Likes

Have you considered making it work with touch at all?

1 Like

Ah yes, I’ll add that in a second

Finished it, it should work with mobile and etc.

Just a question. Why should I use this over Simple Modules For Creating Draggable GUI Elements?

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?

1 Like

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

This is currently showing a better alternative to another snippet, I will be making a module of it very soon.

I prefer:

Because its a module it would you should make yours a module.

Yes, working on that.

Probrably going to add more functions.