SelectionService V.1
Description :
SelectionService is a client sided ModuleScript that will bring to your experience an in-game selection system.
Methods :
:GetSelectedParts() :
Return an array of all parts selected by the client.
:SetFilter(Parts : table, Selectable : boolean) :
Sets the filter that will, if Selectable set to true allow client to select only
passed parts, else it will allow client to select only not passed parts.
Note that you can put non-parts instances and nested tables in the Parts argument,
the module will sort everything to get only parts instances but don't forget to put
all of these things in a table.
:SetBackgroundColor3(Color : Color3) :
Sets selection frame's background color to the passed Color3.
:SetBackgroundTransparency(Transparency : number) :
Sets selection frame's background transparency to the passed number. Note that
transparency goes from 0 to 1.
:SetStrokeColor3(Color : Color3) :
Sets selection frame's stroke color to the passed Color3.
:SetStrokeTransparency(Transparency : number) :
Sets selection frame's stroke transparency to the passed number. Note that
transparency goes from 0 to 1.
:SetStrokeThickness(Thickness : number) :
Sets selection frame's stroke thickness to the passed number.
Events :
.SelectionChanged :
Fired when the client selection changed. Passes an array of all parts selected by
the client.
Properties :
.Enabled :
Value instance that describe if the selection system is enabled or not.
Exemples :
Customize the selection frame :
We want to customize the selection frame because we don’t like the default one.
For this we will need the methods :
-
:SetBackgroundColor3(Color : Color3)
-
:SetBackgroundTransparency(Transparency : number)
-
:SetStrokeColor3(Color : Color3)
-
:SetStrokeTransparency(Transparency : number)
-
:SetStrokeThickness(Thickness : number)
Note that you will have to put the module in ReplicatedStorage and that this is a LocalScript placed in StarterPlayerScripts.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SelectionService = require(ReplicatedStorage:WaitForChild("SelectionService"))
SelectionService:SetBackgroundColor3(Color3.new(0, 0.588235, 1))
SelectionService:SetBackgroundTransparency(0.75)
SelectionService:SetStrokeColor3(Color3.new(0, 0.392157, 1))
SelectionService:SetStrokeTransparency(0.25)
SelectionService:SetStrokeThickness(2)
Setup a basic filter :
We want to setup a filter so every parts in my Folder “SelectableParts” can get selected but all the others can’t.
For this we will need the method :SetFilter(Parts : table, Selectable : boolean).
Note that you will have to put the module in ReplicatedStorage and that this is a LocalScript placed in StarterPlayerScripts.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SelectionService = require(ReplicatedStorage:WaitForChild("SelectionService"))
local Folder = workspace:WaitForChild("SelectableParts")
SelectionService:SetFilter(Folder:GetDescendants(), true)
Highlight selected parts with SelectionBox :
We want all selected parts to get highlighted with a SelectionBox, so we will create one to every part that doesn’t have already one, after we will set it’s visible property to true, and every time the selection change we will set the visible property of all the others to false.
For this we will need the event .SelectionChanged.
Note that you will have to put the module in ReplicatedStorage and that this is a LocalScript placed in StarterPlayerScripts.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SelectionService = require(ReplicatedStorage:WaitForChild("SelectionService"))
SelectionService.SelectionChanged:Connect(function(SelectedParts)
for v, Part in pairs(workspace:GetDescendants()) do
local SelectionBox = Part:FindFirstChildOfClass("SelectionBox")
if SelectionBox then
SelectionBox.Visible = false
end
end
for v, Part in pairs(SelectedParts) do
local SelectionBox = Part:FindFirstChildOfClass("SelectionBox")
if SelectionBox then
SelectionBox.Visible = true
else
local NewSelectionBox = Instance.new("SelectionBox")
NewSelectionBox.Parent = Part
NewSelectionBox.Adornee = Part
NewSelectionBox.Visible = true
end
end
end)
Enable or disable the selection system with the key “b” :
We want to when the client press the key “b”, if the selection system is enabled then disable it, else enable it, to detect inputs we will need UserInputService.
For this we will need the property .Enabled.
Note that you will have to put the module in ReplicatedStorage and that this is a LocalScript placed in StarterPlayerScripts.
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SelectionService = require(ReplicatedStorage:WaitForChild("SelectionService"))
UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
if not GameProcessedEvent and Input.KeyCode == Enum.KeyCode.B then
SelectionService.Enabled.Value = not SelectionService.Enabled.Value
end
end)
I hope that was helpful and have a nice day !
May the force be with you !