What would be the best way to select all the parts behind a Gui Frame?

Here’s a download if anyone is interested:
SelectionBoxLassoTool.rbxl (58.4 KB)

Code:

local ContextActionService = game:GetService('ContextActionService')
local RunService = game:GetService('RunService')

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local SelBox = script.Parent

local hold = false
local x1,y1,x2,y2 = 0,0,0,0

local function comeUpWithAnameForThisFunction(actionName, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then
		hold = true
		x1 = Mouse.X
		y1 = Mouse.Y
	elseif inputState == Enum.UserInputState.End then
		hold = false
	end
end

local function heartbeatConnection()
	if hold == true then
		SelBox.Visible = true

		x2 = Mouse.X
		y2 = Mouse.Y

		SelBox.Size = UDim2.new(0,(x2 - x1), 0, (y2 - y1))
		SelBox.Position = UDim2.new(0,x1,0,y1)
	else
		SelBox.Visible = false
	end
end

local connection = RunService.Heartbeat:Connect(heartbeatConnection)
ContextActionService:BindAction("someNameImNotCreative", comeUpWithAnameForThisFunction, false, Enum.UserInputType.MouseButton1)

wait(10) -- simulating as if I got a bindable event or something

ContextActionService:UnbindAction("someNameImNotCreative")
connection:Disconnect()
SelBox.Visible = false

My Questions:

  1. Can anything be simplified or be done more optimally?

  2. I need a function that sees what parts are currently behind the Gui Frame. Where would the function go? It has to go inside of the heartbeatConnection(), since I want it to constantly update and for the user to see what parts are currently being highlighted, and whether to adjust the Gui Frame’s size or not. I already have a selectPart(part) function and deselectPart(part) function, so I just need to loop through them. How would I even get the parts? I don’t think I can send out thousands of raycasts, one per pixel…

Thanks!

Project all 8 corners into the viewport (WorldToViewportPoint) and if any corners are inside select the part.

What do you mean? For every part in the workspace, create 8 corners, and for each of those corners check? That doesn’t sound like it’s gonna do well each heartbeat…

Won’t know unless you try. There’s ways to make it more efficient but don’t bother unless its actually too slow.

You probably don’t need to do it every heartbeat; just whenever the player resizes the lasso.

Thats all the advice I can give cause I don’t know some of this advanced stuff lol

2 Likes