I want help with making a draggable GUI but I want it to not be able to be dragged off the screen.
I’m not even sure if that’s something you can put in a script but here’s what I have so far:
local UserInputService = game:GetService("UserInputService")
local MainFrame = script.Parent
local RepositioningFrame = MainFrame:WaitForChild("TopBar")
local Camera = workspace:WaitForChild("Camera")
local DragMousePosition
local FramePosition
local Draggable = false
RepositioningFrame.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
Draggable = true
DragMousePosition = Vector2.new(input.Position.X, input.Position.Y)
FramePosition = Vector2.new(MainFrame.Position.X.Scale, MainFrame.Position.Y.Scale)
end
end)
RepositioningFrame.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
Draggable = false
end
end)
UserInputService.InputChanged:Connect(function(input)
if Draggable == true then
local NewPosition = FramePosition + ((Vector2.new(input.Position.X, input.Position.Y) - DragMousePosition) / Camera.ViewportSize)
MainFrame.Position = UDim2.new(NewPosition.X, 0, NewPosition.Y, 0)
end
end)
I have tried a few things. Here’s an example with a GUI that can be dragged but can go off-screen: How to make DRAGGABLE GUIS | Roblox Tutorial - YouTube and I have also looked at developer forum posts but those are the same as the one in the video.
If anyone can help me I’d be grateful.