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 9 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
9 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:

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

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

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"})
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 — 9y
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 — 9y

Answer this question