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

How to perform a function for all parts with the same name?

Asked by 6 years ago
Edited 6 years ago

I have constructed a script that lets you change the color of a part on your model, but when I click it, it only colors one part. What I am aiming for is to click this button and for all parts named "Wing1" to be colored.

local wings1 = game:GetService("ReplicatedStorage").Recolor.Wings1:WaitForChild("a")
wings1.OnServerEvent:Connect(function(player,part)
    local model = player.Character:FindFirstChildOfClass("Model")
    model.Wings.Wing1.Color = Color3.fromRGB(255, 255, 255) 
end)

There are several separate parts named "Wing1" underneath the "Wings" model, and this script is only coloring one of them. How do I color all of them?

EDIT: Another method I tried is:

local wings1 = game:GetService("ReplicatedStorage").Recolor.Wings1:WaitForChild("a")
wings1.OnServerEvent:Connect(function(player,part)
    local Bird = player.Character:FindFirstChildOfClass("Model")
    Bird.Wings:GetChildren("Wing1").Color = Color3.fromRGB(255, 255, 255)   
end)

But this makes the script not work entirely.

2 answers

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

Well, I'm not sure this is the best strategy but, you can try using a for loop and color all the wings like that. Example:

local wings1 = game:GetService("ReplicatedStorage").Recolor.Wings1:WaitForChild("a")
wings1.OnServerEvent:Connect(function(player,part)
    local model = player.Character:FindFirstChildOfClass("Model")
    for i,v in pairs(model:GetChildren()) do
    if v.Name = "Wing1" then
        v.Color = Color3.fromRGB(255, 255, 255) 
    end
    end
end)

This should work. Again, this might not be the best method, but it should work.

If this helped, please accept the answer. Thanks!

0
Hi! I tried this script and I got an error on line 4 saying "Expected ( or { or string, got )" and it underlines the ")" after GetChildren in red. ShinyGriffin 129 — 6y
0
Of course, I see the mistake I made. I edited it, it works now MakeYourEscape 334 — 6y
0
It seems to give me another error, this time under the "=" after v.Name on line 5 saying "Expected 'then' got '=' " ShinyGriffin 129 — 6y
0
He typed in "if v.Name = "Wing1" then" not "if v.Name == "Wing1" then"" GoldenCosmo 64 — 6y
0
Thank you! ShinyGriffin 129 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Write the main code inside the module script and insert a server script on every brick. Just add calling Module script Lines.

Works perfectly.

Answer this question