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

Why won't my local script change the background color smoothly transition into white?

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

5 comments

0

Wait, I am figuring it out

0

Because you aren't giving it intervals to go by, try "for i = 1,255,1"

0

Also a recommendation would be to start using i instead of 1. Better practice other wise you might as well be using a while loop.

0

@Typically, for loops default to iterate by 1 if no value is given

0

@Mr_Pure, it has nothing to do with "good practice". maybe he just wants it to increase by one 255 times. if he used "i" in his addition, it would be doing: g+1, g+2, g+3 ... g+255

3 answers

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


1 comment

0

this could go out of bounds really easily without checks

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

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.

1 comment

0

why dont we just use a plugin to make entire games for us

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