Trying to make a selection box

Trying to make a selection box

game.Players.LocalPlayer:GetMouse().Button1Down:Connect(function()
    startSearching = true
    local rel=workspace.CurrentCamera.CFrame:pointToObjectSpace(game.Players.LocalPlayer:GetMouse().Hit.Position)
    pointBegin=Vector2.new(rel.x/-rel.z, rel.y/-rel.z)
    boxStart = UIS:GetMouseLocation()
end)

UIS.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement and startSearching then
        local currentPos = UIS:GetMouseLocation()
        
        holderFrame.Top.Position = UDim2.fromOffset(boxStart.X,boxStart.Y)
        holderFrame.Right.Position = UDim2.fromOffset(boxStart.X,currentPos.Y)
        holderFrame.Bottom.Position = UDim2.fromOffset(currentPos.X,boxStart.Y)
        holderFrame.Left.Position = UDim2.fromOffset(currentPos.X,currentPos.Y)
        
    end
end)


how it functions atm

Fixed that, though now I have another question:

How would I detect what parts are in the selection box, given the 4 points of the rectangle?

you can raycast every pixel where the gui is covering, if it is too laggy you can skip some pixels

By comparing AbsolutePosition.

Workspace:GetPartsInBox would work. Very performant. If

Well, to do that I need to create the part. My next question would be how would I create a part given 4 points on a 2D surface

I meant, 3D parts inside the UI

There’s a ViewportPointToRay function

Ok then, you can make a 3d raycast using viewportPoint to ray function.
First divide up your selection box into 50 points(can be more or less, just more precise this way).
Then you take these 2d points and use @Tratium’s viewportPointToRay function and get all the unit rays needed.
Then you loop through these unit rays and raycast them.

Alright, so how do I divide it up into X amount of points ?

Take your 1st point(one in the left corner top)
Then take your 2nd point(one in the right corner bottom)
Lerp between your x values at an increment of 7.(could be anything)
Lerp between your y values at an increment of 7.
Iterate up to 7^2(49) and for each 7 switch to using a different y value.

Would there be any other way? like perhaps forming a part based on the 4 points

Definitely it’s workspace:GetPartsInBox.
The 1st thing I suggested.

You don’t need a part, just a limit on how big, and a cframe origin of the box.

How would I get the CFrame?