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

Problem with FindFirstChild?

Asked by
Seraine 103
10 years ago

I'm trying to write a colour changing script that finds every brick if they have "Part" inside their name (Part1, Part2, etc) in workspace. If found then the script will change the bricks' colour to hot pink and then bright red. But the script only work if the bricks each have a different name...

local part = Workspace:FindFirstChild("Part")

while true do 
    if part then
part.BrickColor = BrickColor.new("Hot pink")
wait(0.5)
part.BrickColor = BrickColor.new("Bright red")
end
end

Does anyone know a function that will work for two or more bricks with the same name? Thanks

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

GetChildren is the method that you are looking for. To check for every part in workspace named 'Part' we will have to use a generic for loop to gather all the children in workspace and then use an if statement to find the parts.

local parts = Workspace:GetChildren()

for _, child in ipairs (parts) do -- Generic for loop
    if child.Name == 'Part' then -- If statement
        child.BrickColor = BrickColor.new("Hot pink")
        wait(0.5)
        child.BrickColor = BrickColor.new("Bright red")
    end
end

EDITS

We will need to use two loops to do what you want:

local parts = Workspace:GetChildren()

for _, child in ipairs (parts) do -- Generic for loop
    if child.Name == 'Part' then -- If statement
        child.BrickColor = BrickColor.new("Hot pink")
    end
end
wait(0.5)
for _, child in ipairs (parts) do -- Generic for loop
    if child.Name == 'Part' then -- If statement
        child.BrickColor = BrickColor.new("Bright red")
    end
end
0
It doesn't work exactly like how I wanted it to, but similar. Thanks! Seraine 103 — 10y
0
How can I make the bricks change colour at the same time if they have the same name? Seraine 103 — 10y
0
Check my edit, but be careful because if you have ALOT parts in you're place you may experience a little lag. BlackJPI 2658 — 10y
Ad

Answer this question