Hello, I am currently working on a large project which is only in its conception. And I am stuck on one point, how to make a selection box such as the one found on Roblox studio.
I thought about using the ray cast, but scanning a whole rectangle might not be very efficient, do you have any solution?
This should work perfectly. Once you open this you put it in starter gui then you can start to scale the frame inside of it, pretty sure this is what you where looking for.
Are you doing it on purpose? You just got the roblox interface, what interests me is not how to draw a frame, I think I’m big enough to do it on my own but how to code a system like this.
Oh… Well you can use UserInputService and check when a input begans and if its Enum.UserInputType.MouseButton1 you want to get the mouse position and update the frame each RenderStepped then when it input ends destroy the ui or disable it.
Iterate through the instances, to significantly reduce the amount of parts to loop, you could put the selectable parts in a certain folder
Check whenever the instance is within the rectangle bounds by comparing their positions or using :ToObjectSpace(), if so you could insert the part into an array
Speaking performance-wise, you can still improve a lot and this isn’t any case the optimized way.
I can’t provide any code sample since I’m quite busy, but that’s pretty much how you could conceive that.
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local frame = script.Parent.Frame
frame.Size = UDim2.fromScale(0, 0)
frame.BackgroundTransparency = 0.5 -- Optional
local button1Down = false
local startPos = nil
local endPos = nil
mouse.Button1Down:Connect(function()
button1Down = true
startPos = Vector2.new(mouse.X, mouse.Y)
frame.Position = UDim2.fromOffset(mouse.X, mouse.Y)
frame.Size = UDim2.fromScale(0, 0)
while task.wait() and button1Down do
endPos = Vector2.new(mouse.X, mouse.Y)
frame.Size = UDim2.fromOffset(mouse.X - startPos.X, mouse.Y - startPos.Y)
end
local top = endPos.Y
local bottom = startPos.Y
if startPos.Y < endPos.Y then
top = startPos.Y
bottom = endPos.Y
end
local left = endPos.X
local right = startPos.X
if startPos.X < endPos.X then
left = startPos.X
right = endPos.X
end
local selectedParts = {}
for i, v in workspace:GetChildren() do
local s, e = pcall(function()
local vector, onScreen = camera:WorldToScreenPoint(v.Position)
if (vector.X > left) and (vector.X < right) and (vector.Y < bottom) and (vector.Y > top) then
table.insert(selectedParts, v)
end
end)
end
end)
mouse.Button1Up:Connect(function()
button1Down = false
end)