How to get the part selected

In plugin development, how would I go about getting the object selected in the workspace? For example I create a plugin that outputs the name of the selected object. How is this achieved?

5 Likes

Hi. This can be achieved via the Selection service and it’s Get method. The Get method of the Selection service returns an array of instances currently selected.

Example

-- Studio user selects the Workspace
local selection = game:GetService("Selection")
local current = selection:Get()[1]
print(current.Name) -- Workspace

Likewise, the Set method might be useful if you want to set the currently selected items.

Example

-- Studio user selects the Workspace
local selection = game:GetService("Selection")
selection:Set({game.ReplicatedStorage})

local current = selection:Get()[1]
print(current.Name) -- ReplicatedStorage

Keep in mind that both methods are PluginSecurity and cannot be used in normal Scripts and LocalScripts (and module scripts required from Scripts and LocalScripts. They can be used in a ModuleScript if required from a plugin script)

25 Likes

Brilliant. Thank you so much! :smile:

2 Likes