Help with Draggable Gui Clamp

I am trying to keep this smaller gui frame inside the larger one.

Untitled video - Made with Clipchamp

This is my current script which just lets the point go anywhere

local MousePos = UIS:GetMouseLocation() - Vector2.new(0, game:GetService("GuiService"):GetGuiInset().Y)
	
	local X = (Input.Position.X - Frame.AbsolutePosition.X) / FRame.AbsoluteSize.X
	local Y = (Input.Position.Y - Frame.AbsolutePosition.Y) / Frame.AbsoluteSize.Y
	
	if ButtonDown then
		Point.Position = UDim2.new(X,0,Y,0)
    end

Ive tried other methods like this that somewhat does the job

    local IsY = Frame.AbsolutePosition.Y <= MousePos.Y and MousePos.Y <= Frame.AbsolutePosition.Y + Frame.AbsoluteSize.Y
    local IsX = Frame.AbsolutePosition.X <= MousePos.X and MousePos.X <= Framel.AbsolutePosition.X + Frame.AbsoluteSize.X
	
	if IsY and IsX and ButtonDown then
		Point.Position = UDim2.new(X,0,Y,0)
    end

But the point can still “leak” out the frame
Untitled video - Made with Clipchamp (1)

I know the solution will have to utilize math.clamp but I dont know exactly how to do that.

Thanks if you can help!

1 Like

math.clamp requires 3 numbers. The first number is simply any number you want to clamp. The second and third define the minimum and maximum bounds of a number.

print(math.clamp(3, 1, 5)) -- 3

print(math.clamp(-30, 2, 8)) -- 2. -30 is lower than 2 (minimum)
print(math.clamp(132, 100, 120)) -- 120. 132 is greater than 120 (maximum)

The “leaking” issue isn’t really a leaking issue. That’s just the UI element’s AnchorPoint being set to its center. You can make this go away by simply adding a “UIPadding” object under the big UI frame, and have it set to half of your tiny UI frame’s size.

1 Like

Yup it was the anchor point that was causing the issues. I cant believe I didn’t notice that. Thank you so much.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.