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

Why can't I change the color of multiple parts with this script?

pcb = game.Workspace.F1Standard.PrimaryColoredBlocks:GetChildren()

pcb.BrickColor = BrickColor.new("White")

It's the first time I've done something like this so I'd appreciate the help!

2 answers

✓ Accepted answer

GetChildren returns a table with all the parts in it. If you want to access one of them, you can do pcb[1] to access the first one.

But, what if you wanted to access all of them, Spongo?

Glad you asked. The way that you do this is by using a for loop! A for loop is very helpful in these cases as they loop through each value in your table.

For example, doing this:

pcb = game.Workspace.F1Standard.PrimaryColoredBlocks:GetChildren()

for _,part in pairs(pcb) do --Loops through each value in the table.
	print(part.Name) --If the value is an object, you can access its properties from this for loop.
end

Will print out all the parts' names that are in the table. See how I used the Name property? You can do this for all properties, too!

Now we know we can use properties when looking through a table of objects, we can then apply what you're trying to do: Set the BrickColor to White.

pcb = game.Workspace.F1Standard.PrimaryColoredBlocks:GetChildren()

for _,part in pairs(pcb) do --Loops through each value in the table.
	part.BrickColor = BrickColor.new("White") --Since you can access the properties of an object in a table, we can set the BrickColor of the parts!
end

But what if there are some objects that aren't parts in there?

You can use an if statement to filter out anything that can cause an error, like so:

pcb = game.Workspace.F1Standard.PrimaryColoredBlocks:GetChildren()

for _,part in pairs(pcb) do --Loops through each value in the table.
	if part:IsA("BasePart") then --Checks if the value in the table is a part before continuing.
		part.BrickColor = BrickColor.new("White")
	end
end

That script will work, but there's an easier way of setting the BrickColor to White.

pcb = game.Workspace.F1Standard.PrimaryColoredBlocks:GetChildren()

for _,part in pairs(pcb) do --Loops through each value in the table.
	if part:IsA("BasePart") then --Checks if the value in the table is a part before continuing.
		part.BrickColor = BrickColor.White() --BrickColor.White() is essentially the same thing as what you were doing above.
	end
end

I hope this answer helped you. If it did, be sure to accept it so people know the question has been answered.

5 comments

0

Ninja'd again. Good answer though, BestDeveloper.

0

I tried to make it simple for him to understand the for loop without using in pairs. But good answer still.

0

Thanks.

0

BestDeveloper, your script had for some reason not worked. However, Spongocardo's did. Thank you both for helping me understand how to do this!

0

You're welcome.

You're trying to change all the children in one line but you have to use a for loop to do this!

For loops let you do anything a certain amount of times that you tell it.

How to write a for loop :

for i = 1,20 do

end

On line 1 where it says 'for' that is telling the script that it is a for loop.

On line 1 where it says 'i = 1,20' i is telling the script to do i 20 times

This is not always 20 it can be any number.

If you where going to change i to do a different number you would change the 20 to lets say 100.

Now in your case you want to change the colors.

Putting for loops into use in your code :

pcb = game.Workspace.F1Standard.PrimaryColoredBlocks:GetChildren() -- gets the children

for i = 1,#pcb do -- tells the script to do i how ever many times that there are things in pcb
	if pcb.ClassName == "Part" then -- make sure it is a part
		pcb[i].BrickColor = BrickColor.new("White") -- change the color of the part
	end -- end the if statement
end -- end the for loop

There are other ways of doing for loops but this will help you as a beginner!

I hope this helps you!

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