ive always wondered how developers cycle through colors. how do i do that exactley?
how do i make it so it cycles through colors?
1 answer
If you're learning to code, this is actually a great exercise!
Let's say we have a brick (or a Part in technical terms).
brick = Workspace.Part (if you're confused already, I recommend you read this)
We need a list of the colors to run through. This list is called a table. Define it as a table of Strings:
tableOfColors = {"Really red", "Bright blue", "Bright yellow", "Really black", "Institutional white"} (if you're confused, read this)
We know we set a Part's color by doing this:
brick.BrickColor = BrickColor.new("Really red")
So our goal is just to repalce "Really red" with a value from our table. Let's use the first one:
brick.BrickColor = BrickColor.new(tableOfColors[1])
Now we have a red brick, but we used the tableOfColors to get it!
Now we just need to change that 1 so that it counts through to 5, and then resets back to 1. Let's use a loop to do this once per second:
counter = 1
while true do
brick.BrickColor = BrickColor.new(tableOfColors[counter])
counter = counter + 1
wait(1)
end
(If you're confused, read this)
If you run that code, it will work - but it will eventually run into a problem when counter = 6. We only have 5 numbers! Let's add a check to see if we've gone over our limit and reset counter to 1 if we have:
counter = 1
while true do
if (counter == 6) then
counter = 1
end
brick.BrickColor = BrickColor.new(tableOfColors[counter])
counter = counter + 1
wait(1)
end
That's it! The brick will change colors in order!
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
global "Workspace" is deprecated, use global "workspace"
Please do not encourage people to post these questions. You should not be helping with a question where they have not made an effort to look it up/try.
Regardless of the use of the question or 'deprecated' terms, the answer is good content with good intent. If you were out there helping instead of bashing people who made good answers, there would be better content on this site.