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

Smooth Color Changing Brick?

Hello, I need help in creating a Smooth Color Changing Brick. I've seen some tutorials about brick color change but it does not make a smooth transition, it just instantly changes color. I am currently making a gaming chroma keyboard (Gaming keyboard that changes LED color from time to time, making a rainbow effect.)

Go see the PLACE so you see what I mean.

My current script for it is:

while true do
 script.parent.BrickColor = BrickColor.new("Steel blue")
 script.parent.Material = ("Neon")
 wait (3)

 script.parent.BrickColor = BrickColor.new("Bright violet")
 script.parent.Material = ("Neon")
 wait (3)

 script.parent.BrickColor = BrickColor.new("Magenta")
 script.parent.Material = ("Neon")
 wait (3)

 script.parent.BrickColor = BrickColor.new("Neon orange")
 script.parent.Material = ("Neon")
 wait (3)

 script.parent.BrickColor = BrickColor.new("Bright yellow")
 script.parent.Material = ("Neon")
 wait (3)

 script.parent.BrickColor = BrickColor.new("Lime green")
 script.parent.Material = ("Neon")
 wait (3)

 script.parent.BrickColor = BrickColor.new("Toothpaste")
 script.parent.Material = ("Neon")
 wait (3)

end

This is what I've just temporarily used as I am still asking for help.

5 answers

✓ Accepted answer

**This should work for your circumstances, comment if you have any questions about the code **

--[[ 
	 Keep your colors in this table, they have the 
     .Color property which returns their Color3
	 value for tweening
--]]
local colors = {BrickColor.new("Steel blue").Color, 
				BrickColor.new("Bright violet").Color,
				BrickColor.new("Magenta").Color,
				BrickColor.new("Neon orange").Color,
				BrickColor.new("Bright yellow").Color,
				BrickColor.new("Lime green").Color,
				BrickColor.new("Toothpaste").Color} 

local tweenService = game:GetService("TweenService")

script.parent.Material = ("Neon")

-- You don't need to set it to neon each time, only once

function ColorChange(colorToChangeTo)
	
	--[[ 
		 Set Time, and Time2 as the same value, otherwise the tween will 	
		 play through the next time the ColorChange function runs
	--]]
	local tweenInformation = TweenInfo.new(1) -- Time1
	local ColorProperty = {}
	
	ColorProperty.Color = colorToChangeTo
	
	local tween = tweenService:Create(script.Parent,tweenInformation,ColorProperty)
	tween:Play()
	
	wait(1) -- Time2
	
end


while true do

	for i, v in ipairs(colors) do
		
		ColorChange(v)		
		
	end

end

3 comments

0

Thank you so much, this worked for me! :)

0

sorry Zenith, I was literally doing it as you were doing, I only saw your answer after posting, but in this it's less of a hassle only using a single for loop, rather than 2. It's also easier to understand for beginners

0

Yeah, that's true, I agree. IMHO I think that Color3:lerp() is more simple (to me) than Tween because I'm used to Part lerping. I agree on the single loop, though. Although, why do you create a new function for ColorChange? It looks to me as it can just go in the for loop.

while true do
	script.Parent.BrickColor = BrickColor.Random()
	wait(0.05)
end

Is that what you're asking for?

3 comments

0

If this is not desirable, just change each wait() to wait 0.05 seconds.

0

Actually, you WOULD have to do this while setting shades of the desired colors, which creates a neat rainbow effect.

0

Nope, i need a color changing brick that transitions SMOOTHLY, it's like as if it's naturally changing color. Not just from one color to another without a transition.

You can use property tweening to achieve this. This is not gui tweening. You can find a tutorial here.

To create a smooth color-changing brick, I used Color3:lerp().

Here's the code:

local brick = script.Parent

local colors = {} -- Use a table to store the colors
table.insert(colors, BrickColor.new("Steel blue").Color) -- Use Color3 instead of BrickColor
table.insert(colors, BrickColor.new("Bright violet").Color)
table.insert(colors, BrickColor.new("Magenta").Color)
table.insert(colors, BrickColor.new("Neon orange").Color)
table.insert(colors, BrickColor.new("Bright yellow").Color)
table.insert(colors, BrickColor.new("Lime green").Color)
table.insert(colors, BrickColor.new("Toothpaste").Color) 

brick.Material = Enum.Material.Neon

while true do
	
	for i,v in pairs(colors) do
		brick.Color = v -- Use Color3 instead of BrickColor
		local nextColor = colors[i + 1]
		if(nextColor == nil) then -- End of table
			nextColor = colors[1]
		end
		
		wait(0.1)	
		for i = 0, 19 do -- 1 second transition, 20 iterations
			brick.Color = v:lerp(nextColor, i / 20) -- Slowly lerp/transition to next color			
			wait(0.05)
		end
	end
end

Im sorry to bring this back up, but if you'd like something very simple and goes through the whole spectrum, use this!

local Brick = script.Parent -- Put the part you are changing
local speed = 1 -- The speed that the colors change
while true do
	for i = 0,1,0.001*speed do
		Brick.Color = Color3.fromHSV(i,1,1)
		wait()
	end
end

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