Selection system :
1. The settings
- Create a ScreenGui in StarterGui and add a frame to it, in the frame add a UIStroke, the frame will represent the area of selection :
- Check the IgnoreGuiInset property of your ScreenGui and set the transparency of your frame to 1, after that you can change the thickness of your UIStroke to 2 and the color to [255, 255, 255] :
- Create a LocalScript in your frame and you’re done for the settings :
- Create a SelectionBox in every selectable parts and set the adornee to the part you want to be highlighted, the color to [255, 255, 255] the line thickness to 0.1 and uncheck visible :
2. The script
- Remove the
print("Hello World !")
and add the variables that we will need :
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.Camera
local Frame = script.Parent
local Origin = Vector2.new(0, 0) --Where your mouse started to held
local SelectedParts = {} --The table that will store as a dictionary every selected parts
local LastSelectedParts = {} --The table that will store the last selected parts if control is down
- Create the function that will set the point Origin :
local function SetOrigin()
Origin = UserInputService:GetMouseLocation() --Origin is now equal to the mouse position
end
- Create the function that will return a table of every selected parts :
local function GetSelectedParts()
local Parts = table.clone(LastSelectedParts)
local Target = Mouse.Target
Parts[Target] = Target:FindFirstChildOfClass("SelectionBox") and LastSelectedParts[Target] == nil and Target or nil
for v, Part in pairs(workspace:GetDescendants()) do
if Part:IsA("BasePart") then
local MousePosition = UserInputService:GetMouseLocation()
local Position, Visible = Camera:WorldToViewportPoint(Part.Position)
if Part:FindFirstChildOfClass("SelectionBox") and Visible and math.sign(Position.X - MousePosition.X) * math.sign(Position.X - Origin.X) + math.sign(Position.Y - MousePosition.Y) * math.sign(Position.Y - Origin.Y) < 0 then
if LastSelectedParts[Part] then
Parts[Part] = nil
else
Parts[Part] = Part
end
end
end
end
return Parts
end
- Create a function that will update your frame size and position, that will show you selected parts and that will set LastSelectedParts :
local function Update()
if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
local Difference = UserInputService:GetMouseLocation() - Origin
local X, Y = math.abs(Difference.X), math.abs(Difference.Y)
Frame.Size = UDim2.fromOffset(X, Y)
Frame.Position = UDim2.fromOffset(Difference.X > 0 and Origin.X or Origin.X - X, Difference.Y > 0 and Origin.Y or Origin.Y - Y)
Frame.Visible = Difference.Magnitude > 0
for v, Part in pairs(SelectedParts) do
Part.SelectionBox.Visible = false
end
SelectedParts = GetSelectedParts() --function previously created
for v, Part in pairs(SelectedParts) do
Part.SelectionBox.Visible = true
end
else
Frame.Visible = false
if UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
LastSelectedParts = SelectedParts
else
LastSelectedParts = {}
end
end
end
- Now we can connect our functions to events :
RunService.RenderStepped:Connect(Update)
Mouse.Button1Down:Connect(SetOrigin)
3. The result
Don’t forget to tell me if there’s something wrong or if something is bad explained, I hope that was helpful and have a nice day !!!