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

Script for changing all parts that have the same name?

Asked by 6 years ago

In my game we have little blocks that you click on to change hair color. For some context, we use this WigGiver and when you apply a hairstyle it's basically applies as an accessory called "Hat" onto the player. So here's the script we use now (in this case, it turns the hair color to a light blue):

1local function onClicked(playerWhoClicked)
2    playerWhoClicked.Character.Hat.Part.Mesh.VertexColor = Vector3.new(0.05, 0.412, 0.67)
3end
4 
5script.Parent.ClickDetector.MouseClick:connect(onClicked)

As you guys can probably see, it changes the vertex color of a mesh contained within a part called "Part". However, some hairstyles we use have multiple parts called "Part" in them so when we use the hair colorer, it only changes one part of the hairstyle.

What I would need is a script that changes the mesh color of all parts called "Part" within the Hat. I tried using some scripts I found online but I got completely lost and couldn't get them to work.

0
What you can do is loop "hat" and check if the name is "Part", then, you change the Part.Mesh.VertexColor saSlol2436 716 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

All Accoutrement objects ( Hats, Accessory ) require a BasePart named Handle. So you could do this:

01function onClick(plr)
02    local char = game.Workspace[plr.Name]
03 
04    for _, child in pairs(char:GetChildren()) do
05        if child:IsA"Accoutrement" then
06            for _, part in pairs(child:GetChildren()) do
07                if part.Name == "Handle" then
08                    if part:FindFirstChild"Mesh" then -- if a special mesh
09                        part.Mesh.VertexColor = Vector3.new(X, Y, Z) -- do whatever :v
10                    end
11                end
12            end
13 
14        end
15    end
16end
17 
18script.Parent.ClickDetector.MouseClick:Connect(onClick) -- Connect not connect
Ad
Log in to vote
4
Answered by 6 years ago

One thing we do is use a for i, v in pairs() do loop. What this loop does is for index, values in pairs (stuff) do. What you want to do is put all the Parts in a Folder or put the models with a part inside of it inside a folder. Here is an example:

1for _, hat in pairs(game.Workspace.Hats:GetChildren()) do
2    if hat.Name == 'Part' then
3        -- do something with the hat
4    end
5end

Now this means there will be a part individually in a folder in the workspace and the folder must be called "Hats". Now, what if you had a Model and then you had parts inside a model and you wanted to change that?

Well, you could do ANOTHER for i loop.

1for _, hat in pairs(game.Workspace.Hats:GetChildren()) do
2    for _, parts in pairs(hat:GetChildren()) do
3        if hat.Name == 'Part' then
4            -- do something with the hat
5        end
6    end
7end

Hopefully, this helped you understand how to do things that are a child of something. Simply do if statements to check for specific things.

Answer this question