Issue with detecting gui with mouse

whats up fellow dev people

im currently creating an operating system-ish thingy in roblox

Basically, the issue I’m having right now is i want this GUI (the window in the middle of the screen) to be drag-able (by player mouse cursor) in only the shaded red areas. I’ve implemented being able to move the window, but cant figure out how to make it only movable when the cursor is within the proper bounds.

local UIS = game:GetService('UserInputService')

local frame = script.Parent
local part = game:GetService("Workspace")["TVDark (reupload later, many bugs)"]:FindFirstChild("Resolution", true)

local cornerTL = part:WaitForChild('CornerTL') -- these are just the four corners of the screen
local cornerTR = part:WaitForChild('CornerTR')
local cornerBL = part:WaitForChild('CornerBL')
local cornerBR = part:WaitForChild('CornerBR')

local toggle = false

frame.InputBegan:Connect(function(input)
		if script.Parent.Visible == true then
			dragStart = UIS:GetMouseLocation()
			startPos = frame.Position
			toggle = true
			input.Changed:Connect(function()
				if input.UserInputState == Enum.UserInputState.End then
					toggle = false
				end
			end)
		end
	end
end)

UIS.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		if toggle == true then
			local blx = game:GetService('Workspace').CurrentCamera:WorldToScreenPoint(cornerBL.WorldPosition).X
			local brx = game:GetService('Workspace').CurrentCamera:WorldToScreenPoint(cornerBR.WorldPosition).X
			local screen_resolution = game:GetService('Workspace').CurrentCamera.ViewportSize
			local scaling_translation = 0.5 * screen_resolution.X / math.abs(blx - brx)  
			local delta = scaling_translation * (UIS:GetMouseLocation() - dragStart)

			local udim2offx = math.clamp(
				startPos.X.Offset + delta.X, 
				0 - (frame.Parent.Size.X.Offset - frame.Size.X.Offset)/(frame.Parent.Size.X.Offset/frame.Size.X.Offset),
				(frame.Parent.Size.X.Offset - frame.Size.X.Offset)/(frame.Parent.Size.X.Offset/frame.Size.X.Offset)
			)
			local udim2offy = math.clamp(
				startPos.Y.Offset + delta.Y, 
				0 - (frame.Parent.Size.Y.Offset - frame.Size.Y.Offset)/(frame.Parent.Size.Y.Offset/frame.Size.Y.Offset), 
				(frame.Parent.Size.Y.Offset - frame.Size.Y.Offset)/(frame.Parent.Size.Y.Offset/frame.Size.Y.Offset)
			)
			--print('=====')
			--print('startPos.[X,Y].Scale = ['..startPos.X.Scale..','..startPos.Y.Scale..']')
			--print('delta.[X,Y] = ['..delta.X..','..delta.Y..']')
			--print('UIS:GetMouseLocation()[X,Y] = ['..UIS:GetMouseLocation().X..','..UIS:GetMouseLocation().Y..']; dragStart.[X,Y] = ['..dragStart.X..','..dragStart.Y..']')
			--print('udim2off[x,y] = ['..udim2offx..','..udim2offy..']')

			frame.Position = UDim2.new(
				startPos.X.Scale, 
				udim2offx, 
				startPos.Y.Scale, 
				udim2offy
			)
		end
	end
end)

and then the playerGUI looks like this
image

ive tried many things but alas none of them have worked

my brain is total mush right now and any help is greatly appreciated!!!

You could try adding a table that contains the absolutePositions and absoluteSizes of the reddish covered frame

local dragSpace = {
   AbsolutePositions = {Frame1AP, Frame2AP}, -- frame1 and frame2 are the red frames on your screen
   AbsoluteSizes = {Frame1AS, Frame2AS}
}

And then in inputBegan you can add a check that checks if the mouse is any of those positions

for i=1, 2 do -- 2 is the amount of draggable frame positions
    if (dragStart.X < dragSpace[AbsolutePositions][index].X+dragSpace[AbsoluteSizes][index].X and dragStart.X > dragSpace[AbsolutePositions][index].X) and (dragStart.Y > dragSpace[AbsolutePositions][index].Y and dragStart.Y < dragStart[AbsolutePositions][index].Y+dragStart[AbsoluteSizes][index].Y) then
       -- rest of function that allows for moving
    end
end

This should check if the mouse’s position is inside of those allowed frame and if it is then it will have toggle = true or what enables the dragging of the frame in your script. :+1: