Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to change property of all parts with the same name? [Solved!]

How would I change a property (Transparency, CanCollide, etc.) of every part that has the same name? When I try to do this, it only changes one of them.

2 answers

✓ Accepted answer

You could put all the bricks in a model, and then use a for loop to change all of them.

Check this out:

modelOfBricks = game.Workspace.Model --The model with all the bricks in it.
nameOfBricks = "ColorChange" --The name of the blocks that should be changed (keep quotations).

for i,v in pairs(modelOfBricks:GetChildren()) do
	if v.Name == nameOfBricks then
		--Change properties here. I'll put one below as an example.
		v.Transparency = .5 --Changes every brick's transparency to .5 if they have the name.
	end
end

If I helped, make sure to hit the Accept Answer button below my character! :D

2 comments

0

Good solution, but there is a chance that OP won't be able to get all the parts into a model.

0

I'm sorry I couldn't accept both of them, but this is the one I saw first and used it. It worked. Thanks to all of you!

No need to put all the parts in a model as Discern above stated.

function b(loc,name,wantedTrans,can)
 for _,v in pairs(loc:GetChildren()) do
  if v:IsA("BasePart") and v.Name == name  then
   v.Transparency =  wantedTrans
   v.CanCollide = can
   else
  b( v, name, wantedTrans, can )
  end
 end
end

b(Workspace,"PartNameHere",.5,true)

Also, you can make a function to easily choose what properties you want to change directly in the parameters! Using tuples, this is easy!

function changeProps(loc,name,propArray)
	 for _,v in pairs(loc:GetChildren()) do
  if v:IsA("BasePart") and v.Name == name  then
		for prop,value in pairs(propArray) do
			v[prop] = value
		end
   else
 	 changeProps(v,name,propArray)
  end
 end
end

changeProps(workspace,'BasePlate',{Transparency=.5,CanCollide=.2,Name="STEVE"})

2 comments

0

You really shouldn't use ... like that. I also don't understand why you're requiring that v's name isn't "Camera" -- especially since v.Name == name makes that redundant.

0

Oh, you're right. Sorry about that. When I wrote ' and v.Name ~= "Camera" ', I had the :IsA("BasePart") part in my mind. And the reason for the tuple is I was writing the solution a different way, but ended up changing it. I'll edit my post. Cheers, Blue.

This discussion is preserved as it was. New answers, comments, and votes are closed.