Selection
The Selection service controls the Instances that are selected in Roblox Studio.
This service is particularly useful when developing Plugins, as it allows the developer to access and manipulate the current selection.
Currently selected Instances can be obtained and set using the Selection:Get() and Selection:Set() functions. The Selection.SelectionChanged event fires whenever the current selection changes.
For more information on using Selection and Plugins, see Plugin.
Selection is also often used in the command bar, to set hidden properties or run functions for selected Instances.
Note this class only applies to Roblox Studio and has no applicability to games.
Code Samples
The following code sample, when used in a plugin or the command bar, will rotate currently selected BaseParts.
local Selection = game:GetService("Selection")
for _, object in pairs(Selection:Get()) do
if object:IsA("BasePart") then
object.CFrame = object.CFrame * CFrame.Angles(0, math.pi / 2, 0)
end
end
Properties
SelectionThickness
Methods
Add
Parameters
Returns
Get
Returns
Code Samples
local Selection = game:GetService("Selection")
for _, object in pairs(Selection:Get()) do
if object:IsA("BasePart") then
object.CFrame = object.CFrame * CFrame.Angles(0, math.pi / 2, 0)
end
end
local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)
Remove
Parameters
Returns
Set
Parameters
Returns
Code Samples
local Selection = game:GetService("Selection")
local newSelection = {}
for _, object in pairs(workspace:GetDescendants()) do
if object:IsA("BasePart") and object.BrickColor == BrickColor.new("Bright red") then
table.insert(newSelection, object)
end
end
Selection:Set(newSelection)
Events
SelectionChanged
Code Samples
local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)