Need help for a dragging GUI with adornee

Hi guys! Today, I going to make an eyes-dragging GUI (e.g.: eye mechanism GUI), this time I drag with frame instance, but next, it drags out of the GUI with the cursor.

There is an issue with the draggable GUI script:

local UserInputService = game:GetService("UserInputService")
local frame = script.Parent

local dragToggle = nil
local dragSpeed  = 0.01
local dragStart = nil
local startPos = nil

local function updateInput(input)
	local delta = input.Position - dragStart
	local position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
	game:GetService("TweenService"):Create(frame, TweenInfo.new(dragSpeed), {Position = position}):Play()
end

frame.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		input.Changed:Connect(function()
			dragToggle = true
			dragStart = input.Position
			startPos = frame.Position
		end)
	end
end)

frame.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and Enum.UserInputType.MouseMovement then
		dragToggle = false
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		if dragToggle then
			updateInput(input)
		end
	end
end)

How do I solve this issue without dragging the frame out of the GUI?

Using this function you can check if the eyes clip out of the UI. If so, you can stop moving the eyes by returning in updateInput:

local function isClipped(UIObject: GuiObject)
	local parent = UIObject.Parent
	local boundaryTop = parent.AbsolutePosition
	local boundaryBot = boundaryTop + parent.AbsoluteSize

	local top = UIObject.AbsolutePosition
	local bot = top + UIObject.AbsoluteSize

	local function cmpVectors(a, b)
		return (a.X < b.X) or (a.Y < b.Y)
	end

	return cmpVectors(top, boundaryTop) or cmpVectors(boundaryBot, bot)
end

Credit:

1 Like

Thanks, but I used this function and which I can use the UIObject: GuiObject? Please give me an example.

The UIObject would be the eyes that you click, I’m not sure which variable that is in your code.

You could try this:

local UserInputService = game:GetService(“UserInputService”)

local Frame = script.Parent

local leftMouseButtonHeld

— Insert Rest Of Variables

UserInputService.InputBegan:Connect(function(input)

if input.UserInputType == Enum.UserInputType.MouseButton1 and leftMouseButtonHeld == false then

leftMouseButtonHeld = true

while leftMouseButton == true do

— Insert Drag Code Here

end

end

end)

UserInputService.InputEnded:Connect(function(input)

if input.UserInputType == Enum.UserInputType.MouseButton1 and leftMouseButtonHeld == true then

leftMouseButtonHeld = false

while leftMouseButton == false do

— Insert Stop Code Here

end

end

end)

You could look into using math.clamp() when setting the position.

Documentation

1 Like