b=script.Parent while true do wait(1) num=math.random(1, 3) if num == 1 then b.BackgroundColor3 = Color3(255,0,0) if num == 2 then b.BackgroundColor3 = Color3(255,125,0) if num == 2 then b.BackgroundColor3 = Color3(255,255,0) if num == 3 then b.BackgroundColor3 = Color3(0,255,0) end end end end end
This is my code. I do not know why it is not working. I am a beginner coder and I would like a work around this so I can improve. :)
It is supposed to change a button GUI's background color to different colors in the rainbow, however, the colors do not change. The script analysis says there are no errors.
Thanks!
You would need to use Color3.new()
but if you do that you would have to divide all your values by 255. To avoid that annoyance you can use Color3.fromRGB()
ex. b.BackgroundColor3 = Color3.new(0,255/255,0)
ex b.BackgroundColor3 = Color3.fromRGB(0,255,0)
https://wiki.roblox.com/index.php?title=API:Color3
Also your if statements are set up wrong. It looks like you're trying to set up an if-else statement.
If you want to keep all your ifs, the format would be like the following:
if condition then -- something end if condition2 then -- something end
Or you could use an else statement:
if condition then -- something elseif condition2 then -- something elseif condition3 then -- something end
Furthermore, you are checking if num
equals 2 twice and they are both different values, but that's just a logic error.
You can use this script:
b=script.Parent b.BackgroundColor3 = Color3.new(255,0,0) wait(1) b.BackgroundColor3 = Color3.new(255,125,0) wait(1) b.BackgroundColor3 = Color3.new(255,255,0) wait(1) b.BackgroundColor3 = Color3.new(0,255,0)