I want to learn how to use game:GetService(“Selection”) but in the developer hub there are not many examples.
If you can give me examples would be very kind.
Selection:Get() returns a table of all of the objects you have selected in Studio.
https://developer.roblox.com/en-us/api-reference/class/Selection
Then how can I: (example) print the name of the instance I am selecting?
I’m not on my computer right now. I’ll tell you in a minute.
Before anything, you should check out Selection | Roblox Creator Documentation.
From me reading this documentation,
The Selection service controls the Instances that are selected in Roblox Studio.
So with that being said, you can actually manipulate the client’s selection. As it’s said in the documentation, this is usually used in Plugins.
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
Above is an example of changing the part’s CFrame from the player’s selection. Not my script, it’s from the documentation.
There’s three types of functions that are relevant for this scenario:
-
Selection:Get() – This is used to make a table of the current clients selection.
-
Selection:Set(object) – This is used to force the client to select the object(s) listed inside of the parenthesis.
-
Selection.SelectionChanged – This is an event you use when the client changed what items they were selecting.
For your example, to print a part name of the instance you can do this:
local Selection = game:GetService("Selection")
for _,instance in ipairs(Selection:Get()) do
print(instance.Name)
end
@edozune kinda beat me to it. If you only want to get one selected object, use Selection:Get()[1]
. This will return the first selected object.
I may have beaten you to writing an entire article (sorry about that!), you still was able to let him know how to get the first instance of the clients selection. Nice job!
It doesn’t work : /
(Selection:Get()[1]) ??
Does it error? Not run? Make sure you’re running it in the command bar or in a plugin
From the document, it did say that it would be often use in the command bar. But, it didn’t say that it wouldn’t work from there. So, it can be how I coded it. Try pairs
instead of ipairs
.
Plugin | script
But it doesn’t work
I haven’t read all of the replies, but there seems to be nothing wrong with this code when I immediately look at it. Just ensure you have something actually selected in Studio, otherwise, it will obviously not work.
I want to insert an instance to the selected instance.
Try running the script and put a print at the end to indicate it worked, what results does it give?
A few examples i use a lot in plugins
local Selection = game:GetService("Selection")
print(Selection:Get())-- prints the array of all selected instances, turn off log mode in output to see table instead of table memory address
local Selection = game:GetService("Selection")
Selection:Remove(Selection:Get()) -- deselects all instances that are selected
local Selection = game:GetService("Selection")
local otherInstances = {workspace.Baseplate}
Selection:Add(otherInstances) --If your selecting lets say basepart called Part1, this will add Baseplate to your selection. (Now you are selecting Part1 and Baseplate)
Selection:Set(otherInstances) --This will deselect everything in the selection and set anything in the table to be selected. (Now you are only selecting Baseplate)
You found the solution to this. Thank you!
Thank you! This will be very helpful.
I tried but… nothing, maybe I’m doing something wrong but anyway, thank you!
And if I select 2 or more instances?