Selection

Afficher les obsolètes
création impossible
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.

Échantillons de code

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

Résumé

Propriétés

Méthodes

  • Add(instancesToAdd : Objects):void
    sécurité des plugins
  • sécurité des plugins

    Returns an array of currently selected Instances in Roblox Studio.

  • Remove(instancesToRemove : Objects):void
    sécurité des plugins
  • Set(selection : Objects):void
    sécurité des plugins

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

Évènements

Propriétés

SelectionThickness

lecture uniquement
non répliqué
lecture parallèle

Méthodes

Add

void
sécurité des plugins

Paramètres

instancesToAdd: Objects

Retours

void
sécurité des plugins

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().


Retours

An array of currently selected Instances.

Échantillons de code

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

void
sécurité des plugins

Paramètres

instancesToRemove: Objects

Retours

void

Set

void
sécurité des plugins

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:


local selected = Selection:Get()
table.insert(selected, object)
Selection:Set(selected)

Paramètres

selection: Objects

An array of Instances to set the current selection to.


Retours

void

Échantillons de code

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)

Évènements

SelectionChanged

Fires when the Instances selected in Roblox Studio changes.

Note this event does not give the new selection. Developers will need to use the 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.


Échantillons de code

Selection.SelectionChanged

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