Using a table in the command bar

Hi, I have a bunch of windows (30+) and want to give every single one of them a selection box. To speed things up, I want to use the selection service in the command bar. I want to select all the adornee’s and set it to their parent (the window).

How i want to achieve my goal

  1. I paste my code in the command bar
  2. I select all the selection boxes in my explorer
  3. I hit enter on the command bar and the computer automatically sets all the selection box adornees to their parents (the window)

The output says it’s expecting a table, but i am already giving it a table. Here is my code

local selection = game.Selection:Get()
local allSelectionBoxes = selection[#selection]

for i , v in pairs (allSelectionBoxes) do
	i.Adornee = i.Parent
end

^ I’ve tried using both i and v but doesn’t work.

In the Output window, i get:
“:4: invalid argument #1 to ‘pairs’ (table expected, got Instance)”

thank you

1 Like

Probably you need to use :GetChildren

for i, v in pairs (game.Selection:GetChildren()) do
	v.Adornee = i.Parent
end

This is actually storing the last element in the list, so you’re passing an instance into your pairs function. game.Selection:Get() is already a table. Just pass selection into your iterator function.

You’re also referencing your instances i, which would be the index number (key) in the table, not the instance. You want to use the second parameter (v), which I’ve renamed here for clarity:

local selection = game.Selection:Get()

for _, instance in ipairs(selection) do
	instance.Adornee = instance.Parent
end
1 Like

Thank you so much :slight_smile:

The only minor typo in your code was

Here is the final code

local selection = game.Selection:Get()

for _, instance in ipairs(selection) do
	instance.Adornee = instance.Parent
end
1 Like