How to make multiple parts start flashing random chosen colors when a separate part is touched

Started scripting about a week ago and what I’m trying to do is make it to where when the red part (X) is touched, the other parts will start flashing about 4 different colors of my choice at random & simultaneously.

Ideally, I need the colors to be [31, 56, 36] , [49, 89, 57] , [59, 107, 68] and [29, 52, 33]

When I was trying this, the only thing I could think about doing was to make a script (parent = X) for each part labeled a-p but I know there’s an easier more effective way to do it.

Here is the script for one individual part: (a)

script.Parent.Touched:Connect(function()


	while true do
		workspace.flashingparts.a.Color = Color3.fromRGB(59, 106, 67)
		wait(1)
		workspace.flashingparts.a.Color = Color3.fromRGB(47, 85, 54)
		wait(1)
	end
end)

(Tried doing Color3.fromRGB, but I don’t think it’s necessary.)
(Ignore the colors I use in the script, I was just testing it out)


Also, as you can see in the video, it will fire once when I touch the red part the first time, and then fire a bunch more times the longer I stand on it. I just need it to happen once and never again.

2 Likes

For the randomness of the colours, you could put them in an array and pick a random number then use the colour that the number responds to. To stop it firing multiple times, you could use a debounce which is a local variable set to false, then when the red part is touched, checks if it is false, then sets it to true so that it can’t be fired many times over. You usually have a condition to set it back to false, such as a certain time waited or getting off the part.

2 Likes

Here is some code I made:

local Parts = workspace.flashingparts:GetChildren()
local Colors = {
	Color3.fromRGB(31, 56, 36),
	Color3.fromRGB(49, 89, 57),
	Color3.fromRGB(59, 107, 68),
	Color3.fromRGB(29, 52, 33)
}

local RNG = Random.new()

script.Parent.Touched:Once(function()
	while true do
		for _,Part in pairs(Parts) do
			Part.Color = Colors[RNG:NextInteger(1, #Colors)]
		end
		wait(1)
	end
end)

You only need one of these scripts inside of the button.
I will explain all the parts of the script.

local Parts = workspace.flashingparts:GetChildren() - It creates an array(a list) of all the parts.

local Colors = {
	Color3.fromRGB(31, 56, 36),
	Color3.fromRGB(49, 89, 57),
	Color3.fromRGB(59, 107, 68),
	Color3.fromRGB(29, 52, 33)
}

-This creates an array of all the colors.

local RNG = Random.new() - This defines a new Random object, which is of course used to generate random numbers.

script.Parent.Touched:Once(function()
	.........
end)

-The Once function will only trigger the Function inside once, and then never again.

for i,Part in pairs(Parts) do
	.........
end

-This will iterate through all the parts in the Parts array, and run the code inside for each object in the array. It gives two arguments, in this case named i and Part. i is the index of the object, which means what position it has in the array. Part is the object itself.

Part.Color = ... of course sets the Color of the Part.

Colors[x] will take the value at index x in the array. For example, Colors[2] would return Color3.fromRGB(49, 89, 57) because it’s the second object in the array.

RNG:NextInteger(1, #Colors) will generate an integer(a whole number, no decimals) between 1 and the length of the Colors array(written by #Colors)

3 Likes

Oh yeah, and for all of them changing colour at once, I would put them all in a certain folder or model, use a :GetChildren() function the do

local parts = workspace.Parts:GetChildren() --folder/model with parts that change colour

for i, v in tab do
     --code to change all the colours (v.Color = Color3.fromRGB(31, 56, 36)) for example
end
2 Likes

very helpful, ty

question: if i want to add another event to the touch part, how would i do it? where would i put the code?

example: touching red part will cause parts to start flashing AND make workspace.SOUND.PlaybackSpeed = 10

1 Like

Sorry I missed your message, you have to put it inside the Touched function, but before the while true loop. Code placed after a while loop will not run after the while loop is complete, which in this case, will never finish.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.