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 5 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):

local function onClicked(playerWhoClicked)
    playerWhoClicked.Character.Hat.Part.Mesh.VertexColor = Vector3.new(0.05, 0.412, 0.67)
end

script.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 — 5y

2 answers

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

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

function onClick(plr)
    local char = game.Workspace[plr.Name]

    for _, child in pairs(char:GetChildren()) do
        if child:IsA"Accoutrement" then
            for _, part in pairs(child:GetChildren()) do
                if part.Name == "Handle" then
                    if part:FindFirstChild"Mesh" then -- if a special mesh 
                        part.Mesh.VertexColor = Vector3.new(X, Y, Z) -- do whatever :v
                    end
                end
            end

        end
    end
end

script.Parent.ClickDetector.MouseClick:Connect(onClick) -- Connect not connect
Ad
Log in to vote
4
Answered by 5 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:

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

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.

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

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