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. For example:


1game.Selection:Get()[1]:SetPrimaryPartCFrame(CFrame.new()) -- move the selected model to the origin
2

Note this class only applies to Roblox Studio and has no applicability to games.

Code Samples

Selection

1local Selection = game:GetService("Selection")
2
3for _, object in pairs(Selection:Get()) do
4 if object:IsA("BasePart") then
5 object.CFrame = object.CFrame * CFrame.Angles(0, math.pi / 2, 0)
6 end
7end

Summary

Properties

HIDDEN
READ ONLY
NOT REPLICATED
HIDDEN
READ ONLY
NOT REPLICATED
READ ONLY
NOT REPLICATED

Events


Fires when the Instances selected in Roblox Studio changes.

Methods

Add(instancesToAdd: Objects): void  


Get(): Objects  

Returns an array of currently selected Instances in Roblox Studio.

Remove(instancesToRemove: Objects): void  


Set(selection: Objects): void  

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

SetTerrainSelectionHack(center: Vector3, size: Vector3): void  


Properties

ActiveInstance

Hidden
Read Only
Not Replicated

SelectionLineThickness

Hidden
Read Only
Not Replicated

SelectionThickness

Read Only
Not Replicated

Events

SelectionChanged

Fires when the Instances selected in Roblox Studio changes.

Note this event does not give the new selection. Developers will need to use Selection's Selection:Get() function to obtain the current selection.

Although this event can be used outside of plugins and the command bar, it only applies to the selection in Roblox Studio and therefore has no functionality outside of Studio.

To change the selection use the Selection:Set() function.


Code Samples

Selection.SelectionChanged

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

Methods

Add

void
Plugin Security

Parameters

instancesToAdd: Objects

Returns

void

ClearTerrainSelectionHack

void
Roblox Script Security

Returns

void
Plugin Security

Returns an array of currently selected Instances in Roblox Studio.

If no Instances are selected, the array returned be empty. This function can be used in conjunction with the Selection.SelectionChanged event to get the selection whenever it changes.

Note, this function can only be used in Plugins or the command line.

For changing the current selection, please see Selection:Set().


Returns

An array of currently selected Instances.

Code Samples

Selection.SelectionChanged

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

1local Selection = game:GetService("Selection")
2
3for _, object in pairs(Selection:Get()) do
4 if object:IsA("BasePart") then
5 object.CFrame = object.CFrame * CFrame.Angles(0, math.pi / 2, 0)
6 end
7end

Remove

void
Plugin Security

Parameters

instancesToRemove: Objects

Returns

void

Set

void
Plugin Security

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

Calling this function will cause the Selection.SelectionChanged event to fire, unless the new selection set is identical to the previous selection.

Note this function overwrites the existing selection. However, using Selection:Get() an Instance can be added to the existing selection like so:


1local selected = Selection:Get()
2table.insert(selected, object)
3Selection:Set(selected)
4

Parameters

selection: Objects

An array of Instances to set the current selection to.


Returns

void

Code Samples

Selection Set

1local Selection = game:GetService("Selection")
2
3local newSelection = {}
4for _, object in pairs(workspace:GetDescendants()) do
5 if object:IsA("BasePart") and object.BrickColor == BrickColor.new("Bright red") then
6 table.insert(newSelection, object)
7 end
8end
9
10Selection:Set(newSelection)

SetTerrainSelectionHack

void
Roblox Script Security

Parameters

center: Vector3
size: Vector3

Returns

void