Why won't my local script change the background color smoothly transition into white? I'm trying to get my red background become white, but all it does is immediately turn black?
LocalScript:
for i = 1, 255 do wait(0.1) local r = script.Parent.Background.BackgroundColor3.r local g = script.Parent.Background.BackgroundColor3.g local b = script.Parent.Background.BackgroundColor3.b wait(0.01) print(r.. g.. b) script.Parent.Background.BackgroundColor3 = Color3.fromRGB(r,g+1,b+1) end
In order to do this without it going out of bounds, you will need to use a few checks. You do have the basics down though.
local color = script.Parent.Background.BackgroundColor3 --variables are fun, use them local r = script.Parent.Background.BackgroundColor3.r local g = script.Parent.Background.BackgroundColor3.g local b = script.Parent.Background.BackgroundColor3.b for i = 1, 255 do wait(0.1) --now that it is simplified you only need 1 wait() g = i --if i is going to increase from 1 to 255, might as well assign the g and b values to i so you don't have to do extra math. b = i print(r.. g.. b) color = Color3.fromRGB(r,g,b) --while it is true that you could just write 255 instead or "r", I'd still use the variable r in the case that you decide to change this in the future. end
Now if the r/g/b values are all different numbers, it gets a bit more complicated, but not by much:
local color = script.Parent.Background.BackgroundColor3 --variables are fun, use them local r = script.Parent.Background.BackgroundColor3.r local g = script.Parent.Background.BackgroundColor3.g local b = script.Parent.Background.BackgroundColor3.b local red = false local green = false local blue = false for i = 1, 255 do wait(0.1) if r ~=255 and not red then r = r + 1 else red = true end if g ~=255 and not green then g = g + 1 else green = true end if b ~= 255 and not blue then b = b + 1 else blue = true end print(r.. g.. b) color = Color3.fromRGB(r,g,b) if red and green and blue then break -- the red green blue check allows it to stop looping early if it reached a full white before the loop was finished. end end
This should work for you. it worked for me. I don't really know how to explain why yours wasn't working, though it should be fixed. It kept printing out a lot of weird decimals. I don't know much about R G B, so I don't know why it was printing out a bunch of crazy decimals. Maybe you weren't adding 1 to the value the proper way. I think that is why yours was broken, so I added them using a different method. Here you go.
local g = script.Parent.BackRound.BackgroundColor3.g local b = script.Parent.BackRound.BackgroundColor3.b for i = 1, 255 do wait(0.1) g = g + 1 b = b + 1 print("255 ".. g.. b) script.Parent.BackRound.BackgroundColor3 = Color3.fromRGB(255, g, b) end
You can use i but there is a library/plugin you can use to do this.
You can use the RoStrap plugin to tween many properties that can’t be tweened using Roblox’s TweenService and it is also smoother. I’d recommend getting the plugin and reading the documentation to easily tween color.