Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 10 years ago

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

Log in to vote
4
Answered by
Discern 1007 Moderation Voter
10 years ago

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

Check this out:

1modelOfBricks = game.Workspace.Model --The model with all the bricks in it.
2nameOfBricks = "ColorChange" --The name of the blocks that should be changed (keep quotations).
3 
4for i,v in pairs(modelOfBricks:GetChildren()) do
5    if v.Name == nameOfBricks then
6        --Change properties here. I'll put one below as an example.
7        v.Transparency = .5 --Changes every brick's transparency to .5 if they have the name.
8    end
9end

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

0
Good solution, but there is a chance that OP won't be able to get all the parts into a model. DigitalVeer 1473 — 10y
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! DrCylonide 158 — 10y
Ad
Log in to vote
6
Answered by 10 years ago

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

01function b(loc,name,wantedTrans,can)
02 for _,v in pairs(loc:GetChildren()) do
03  if v:IsA("BasePart") and v.Name == name  then
04   v.Transparency =  wantedTrans
05   v.CanCollide = can
06   else
07  b( v, name, wantedTrans, can )
08  end
09 end
10end
11 
12b(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!

01function changeProps(loc,name,propArray)
02     for _,v in pairs(loc:GetChildren()) do
03  if v:IsA("BasePart") and v.Name == name  then
04        for prop,value in pairs(propArray) do
05            v[prop] = value
06        end
07   else
08     changeProps(v,name,propArray)
09  end
10 end
11end
12 
13changeProps(workspace,'BasePlate',{Transparency=.5,CanCollide=.2,Name="STEVE"})
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. BlueTaslem 18071 — 10y
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. DigitalVeer 1473 — 10y

Answer this question