Selection

Show Deprecated
Not Creatable
Service

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.

Selection

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

Summary

Methods

  • Add(instancesToAdd : Instances):()
    Plugin Security
  • Get():Instances
    Plugin Security

    Returns an array of currently selected Instances in Roblox Studio.

  • Remove(instancesToRemove : Instances):()
    Plugin Security
  • Set(selection : Instances):()
    Plugin Security

    Sets the currently selected objects in Roblox Studio to Instances in the given array.

Properties

SelectionThickness

Read Only
Not Replicated
Read Parallel

Methods

Add

()
Plugin Security

Parameters

instancesToAdd: Instances

Returns

()

Get

Instances
Plugin Security

Returns

Instances

Code Samples

Selection

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
Selection.SelectionChanged

local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)

Remove

()
Plugin Security

Parameters

instancesToRemove: Instances

Returns

()

Set

()
Plugin Security

Parameters

selection: Instances

Returns

()

Code Samples

Selection Set

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

Selection.SelectionChanged

local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)