Here is how you can give a part a color changing effect in which the part would gradually go through all of the colors of the rainbow. We will use a simple loop script inserted into any part to make the rainbow effect happen.
**NOTE: There is a TL;DR section at the bottom in case you want the full code without following the steps behind the code.
Insert any part into the workspace in ROBLOX studio by clicking on the "Part" in the ribbon on the top. It can be any shape you would like. Insert a script by right clicking the part you just made in the explorer tab and clicking "Insert Object" -> "Script".
Coding the part
Inside the script you created, let's begin laying out our variables:
b = script.Parent
-- refers to the Part as it is a parent of the script
x = 0 -- this is a nummerical value. We will get to this later as we create a looping function in the next step.
As we have our variables laid out, how can we give the rainbow effect to the parent part? To accomplish this, we will use a "while true do" loop which is an infinite loop that runs. As we see later, this loop lets these values we defined earlier change over time, and that these values could be used to change one of the properties of the part.
b = script.Parent x = 0 while true do wait()
IMPORTANT: You must add a " wait() " function or else the code would run infinitely can cause an explosion! (Not true, but make sure you add the wait() or else it may crash the game)
Coding the loop
So if you would want to change the color of the brick though all the rainbow's colors, we will change the color of the brick by changing the hue value:
while true do b.Color = Color3.fromHSV(x,1,1)
-- the three parameters inside the brackets represent the hue, the saturation, and the value in order from left to right.
x = x + 1/255
-- We change the x value as this loop repeats which is responsible for the rainbow effect.
Basically, what this loop does is that this block of code will run in a loop and as we do it, the color of the brick will be re-defined with a new x-value for the hue (the first parameter inside the bracket).
The x - value will increase by adding 1/255 (since the color values are in 255 scale) everytime it loops. It basically shifts the hue of the part, which makes the rainbow effect happen.
It's red again!
Eventually, the brick will change color back to red, which is where the color of the part was in the first place. To make sure the cycle repeats again, we use an if statement to change the x value back to 0.
b = script.Parent x = 0 while true do b.Color = Color3.fromHSV(x,1,1) x = x + 1/255 if x >= 1 then
-- when the x value reaches a value equal or greater than 1 (which is = to 255/255), the value will reset back to 0.
x = 0 end wait() end
This is done so that it can repeat the rainbow cycle endlessly once the brick/part goes back to red.
===================================== TL;DR =========================================
This is the final code in complete. Make sure the script is inside the Part as a child. Once again, do not forget the wait!!!
b = script.Parent x = 0 while true do b.Color = Color3.fromHSV(x,1,1) x = x + 1/255 -- you can change the increment to speed/slow up the changing effect (eg: 3/500, 10/255, 0.5/255, etc.) if x >= 1 then x = 0 end wait() -- put time in seconds to slow the effect if you want (rlly slowwwwwwwwwwwwww). Leave nothing inside otherwise. end
Thanks for reading this seemingly boring and lengthy tutorial. I hope you enjoyed it. Please note that I am still a beginner at scripting and that this is not the best piece of code out there. Enjoy your rainbow brick.
Easy Written By kyle993
See kyle993's profile on Roblox