Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Problem with FindFirstChild?

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

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

3 comments

0

It doesn't work exactly like how I wanted it to, but similar. Thanks!

0

How can I make the bricks change colour at the same time if they have the same name?

0

Check my edit, but be careful because if you have ALOT parts in you're place you may experience a little lag.

This discussion is preserved as it was. New answers, comments, and votes are closed.