How to select children with same names?

Hello!

I’m trying to select every part with the same name and change their size. How do I do this?

(I know how to size, I just don’t know how to select all and I don’t wanna make tons of variables)

6 Likes

You will use a for loop in a Instance. Do get the children, just do Instance:GetChildren.

for index,value in ipairs(Instance:GetChildren()) do
-- stuff
end

Now, what is index and value? If you know what a table is, that’s what it is. The index represents the position of the value. GetChildren returns an array consisting of all the childrens in an Instance.

2 Likes

As @Quwanterz has said you will need to use a for loop to filter though your workspace and detect a parts name. Your code could look something like this:

for _, v in pairs (game.workspace:GetDescendants())do
if v.Name == 'Part' then -- change part to the name you want to look for
-- Code here will work on anything in workspace named part 
end
end
17 Likes

Oh wait this would work too, right?

for index, descendant in pairs(descendants) do
	if descendant:IsA("BasePart") then
		descendant.BrickColor = BrickColor.Green()
	end
end
3 Likes

Yes that would work but that will check for the type of the part rather than the name of it

1 Like

What would the parts variable be? How do I refer to them?

Nevermind, I got it. It’s v.Color

1 Like

In my code v is equal to each part that returns true so referting to v after if v.Name will be referring to each part that has met the conditions stated

1 Like

I’m trying but it doesn’t work:

for i,decal in pairs(game.Workspace:GetDescendants()) do
if decal.Name == ‘Eyes Decal’ or ‘Mouth Decal’ then
decal.Name = decal.Name + decal.Parent.Parent.Parent.Name
end
end

Is there maybe a specific way I need to run it or place the script? I want it to work outside of game too

(Edit: Ah I just realized this post is 3 years old nevermind)

the or that you put is basically saying “if name is ‘Eyes Decal’ or false do this”.
I believe what you want is “if the name is ‘Eyes Decal’ or the name is ‘Mouth Decal’ do this”
Like this:

for i, decal in ipairs(game.Workspace:GetDescendants()) do
	if decal.Name == "Eyes Decal" or decal.Name == "Mouth Decal" then
		-- Insert Code Here
		decal.Name = decal.Name + decal.Parent.Parent.Parent.Name
	end
end

Also you may want to double check that the Object which is third parent above the Decal actually exists and is the correct object.

oh and feel free to change it back to pairs, idk the difference between ipairs and pairs much, i just prefer to use ipairs.

I wanted mine to be lights that would flash in a loop but only one of them actually flashes

for index, spotlight in ipairs(workspace.Spotlights:GetDescendants()) do
	if spotlight:IsA("SpotLight") then
		while true do
			spotlight.Enabled = true
			wait(0.01)
			spotlight.Enabled = false
			wait(math.random(1,4.5))
		end
	end
end

It went something like that. Is there a way I could make all the lights flash instead of just one?

your wait loop makes it so that only the first spotlight found flickers. try using coroutines to run them in sync instead, like this:

function flicker(spotlight)
	while true do
		spotlight.Enabled = true
		wait(0.01)
		spotlight.Enabled = false
		wait(math.random(1,4.5))
	end
end

for index, spotlight in ipairs(workspace.Spotlights:GetDescendants()) do
	if spotlight:IsA("SpotLight") then
		flicker_wrap = coroutine.wrap(flicker)
		flicker_wrap(spotlight)
	end
end