How to make a "Drag to select" system for a Building Tools-esque gear?

I’m trying to make a little system where, if you hold the mouse down it will make a little frame that adjusts size in X and Y, then tries to find any part in workspace and print out said part’s name.

Issue is: It will NEVER find ANYTHING.

Here’s my code so far:

local dragging = false
local startpos = nil
local selectionbox = nil
local function whenmousedown(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		dragging = true
		startpos = input.Position
		selectionbox = Instance.new("Frame")
		selectionbox.Size = UDim2.new(0, 0, 0, 0)
		selectionbox.Position = UDim2.new(0, startpos.X, 0, startpos.Y)
		selectionbox.BackgroundColor3 = highlightPath.OutlineColor
		selectionbox.BorderSizePixel = 2.5
		local dark = Color3.new(
			highlightPath.OutlineColor.R * 0.5,
			highlightPath.OutlineColor.G * 0.5,
			highlightPath.OutlineColor.B * 0.5
		)
		selectionbox.BorderColor3 = dark
		selectionbox.Transparency = 0.5
		selectionbox.Parent = script.Parent.Parent.Parent.CreateMainFrame
	end
end

local function whenmousemove(input)
	if dragging and selectionbox then
		local delta = input.Position - startpos
		selectionbox.Size = UDim2.new(0, delta.X, 0, delta.Y)
	end
end

local function whenmouseup(input)
	if dragging then
		dragging = false
		local selectboxpos = selectionbox.Position
		local selectboxsize = selectionbox.Size
		local selectboxtl = Vector2.new(selectboxpos.X.Offset, selectboxpos.Y.Offset)
		local selectboxtr = selectboxtl + Vector2.new(selectboxsize.X.Offset, selectboxsize.Y.Offset)
		for _, instance in pairs(workspace:GetDescendants()) do
			local hasproperties = pcall(function()
				local pos = instance.Position
				local a = instance.Size
				local b = instance.CanCollide
			end)
			if hasproperties then
				local instanceposition = instance.Position
				local instancesize = instance.Size or Vector3.new(0, 0, 0)

				local instancetl = Vector2.new(instanceposition.X, instanceposition.Z)
				local instancebr = instancetl + Vector2.new(instancesize.X, instancesize.Z)

				if instancetl.X >= selectboxtl.X and instancetl.Y >= selectboxtl.Y and
					instancebr.X <= selectboxtr.X and instancebr.Y <= selectboxtr.Y then
					print("Instance found within selection box: " .. instance:GetFullName())
				end
			end
		end

		selectionbox:Destroy()
	end
end

game:GetService("UserInputService").InputBegan:Connect(whenmousedown)
game:GetService("UserInputService").InputChanged:Connect(whenmousemove)
game:GetService("UserInputService").InputEnded:Connect(whenmouseup)

It’d probably be good if there was a fix.
There’s also another issue where it’ll also happen if you’re dragging UI and I don’t know how to get rid of that.