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.
You could put all the bricks in a model, and then use a for loop to change all of them.
Check this out:
1 | modelOfBricks = game.Workspace.Model --The model with all the bricks in it. |
2 | nameOfBricks = "ColorChange" --The name of the blocks that should be changed (keep quotations). |
3 |
4 | for 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 |
9 | end |
If I helped, make sure to hit the Accept Answer button below my character! :D
No need to put all the parts in a model as Discern above stated.
01 | function 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 |
10 | end |
11 |
12 | 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!
01 | function 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 |
11 | end |
12 |
13 | changeProps(workspace, 'BasePlate' , { Transparency = . 5 ,CanCollide = . 2 ,Name = "STEVE" } ) |