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