How do I recolor multiple parts at once?

So what I want to achieve?
It is simple I want to recolor multiple parts at once.

The problem is that it only changes colors of a single part at a time not every part in the folder at once.

Yes, I looked everywhere on the devforum, google, just everywhere and I cannot find an solution.

Here is the code I have already written:

local partFolder = script.Parent
local prompt = workspace.yes.Attachment.ProximityPrompt

prompt.Triggered:Connect(function()
	for i, partFolder in ipairs(partFolder:GetChildren()) do
		if partFolder:IsA("BasePart") then
			partFolder.BrickColor = BrickColor.new("Really black")
			wait(5)
			partFolder.BrickColor = BrickColor.new("Institutional white")
		end
	end
end)

Mind fixing the script and telling me what is wrong with it.

Thank you

1 Like

You could use 2 loops.
The first loop paints them all red
Then you wait 5 seconds
The second loop paints them all white

The course of execution of the script is actually not wrong, but it rather lacks a programmatical behavior that might not give results as anticipated.

Have you heard about the coroutine and thread before? If not so, let me take it into consideration.

There exists a library named coroutine that helps you create threads that can be executed in the same environment at the same time within the same processor of the script, meaning there can be multiple threads a script may handle.

What you are suggested to do with the script above is use the coroutine library and separate the part changing colors with another thread. Running the script below will let you visualize how it functions.

local partFolder = script.Parent
local prompt = workspace.yes.Attachment.ProximityPrompt

prompt.Triggered:Connect(function()
	for i, partFolder in ipairs(partFolder:GetChildren()) do
		if partFolder:IsA("BasePart") then
            coroutine.wrap(function()
			    partFolder.BrickColor = BrickColor.new("Really black")
			    wait(5)
			    partFolder.BrickColor = BrickColor.new("Institutional white")
            end)()
		end
	end
end)

External links: coroutine library

1 Like

This one may work, I didn’t test it out yet. Thanks for the suggestion.

While this may have been marked as solved, the solution provided is overkill for what is needed and I personally wouldn’t recommend it. If there’s over 100 parts, that means you’re easily creating 100 threads. Instead, you should be setting the color in a loop, waiting for a bit, and repeating the loop again after the wait like so:

local partFolder = script.Parent
local prompt = workspace.yes.Attachment.ProximityPrompt

local function setPartsColor(newColor: Color3)
	for _,descendant in partFolder:GetDescendants() do
		if descendant:IsA("BasePart") then
			descendant.Color = newColor
		end
	end
end

prompt.Triggered:Connect(function()
	if prompt.Enabled then
		prompt.Enabled = false

		setPartsColor(Color3.fromRGB(0, 0, 0))
		task.wait(5)
		setPartsColor(Color3.fromRGB(255, 255, 255))

		prompt.Enabled = true
	end
end)

As you can see, there’s no need to use multiple threads for a simple task like this one. The setPartsColor function goes through all the descendants of partFolder and if the descendant is a BasePart (an abstract class that includes stuff like Part, MeshPart, WedgePart, ect.), then it’ll change the color of the part to the provided newColor. Your original code uses BrickColor, which isn’t necessarily a bad thing, but you’re better off using a Color3 because you can use more colors. If you want to use a BrickColor still, you can do the following:

setPartsColor(BrickColor.new("Really black").Color)

With such a function, you just need to call it once to change the colors of the parts, wait a bit with task.wait, and then use it again. Coroutines are useful in certain circumstances, but this is one where they don’t need to be used. If you’re interested learning more about creating threads and whatnot, I’d recommend using the task library.

You’ll notice I’m using ProximityPrompt.Enabled for a debounce, but it isn’t necessary for solving your problem.

3 Likes

This works too, thank you also for responding.