I'm trying to make the fogcolor change color but smoothly, it doesnt seem to work. Can you help me please?
while wait(0.1) do script.Parent.FogColor = script.Parent.FogColor - Color3.new(1, 1, 1) end
Well, you can't preform arithmetic operations with what Color3.new() returns (or anything that returns a userdata value). With Color3.new(), we have to use special properties given to use by ROBLOX Lua, called r,g,b (though, these are very generic references to the properties in almost every situation). These properties obviously stand for Red, Green, Blue (which are the 3 arguments Color3.new has)
Another thing is, these colors go from 0-1 (much like the transparency of a part), so we wouldn't subtract 1 from it's color (that would just give us a black color no matter what since Color3.new(0,0,0) is black)
So let's go over these properties of Color3.new real quick:
-- Just like how we'd say X, Y, and Z for Vector3.new, we'd use r, g, and b for Color3.new. local Color = Color3.new(1,0,0) -- Blue color print(Color.r) -- > 1 print(Color.g) -- > 0 print(Color.b) -- > 0
This is how we can "tween" it's color. However, we're also going to need a reference to it's original color so we know what to subtract what from. Using a table to do this would be easier than making 3 separate variables, since we probably don't want "R","G","B" for variable names. So let's say:
local Color = Color3.new(1,1,1) -- White color -- Store all 3 colors in the table with original color values local _Color = { R = Color.r, -- Store the red color G = Color.g, -- Store the green color B = Color.b -- Store the blue color } -- Let's use a for loop instead of a while loop, so we have a default variable that changes upon our increment. -- Don't worry, i'll cover this for i = 1,100 do Color = Color3.new(_Color.R - i / 255, _Color.G - i/255, _Color.B - i/255) wait() end
That last part of the example probably seems pretty confusing. But here, let me explain:
These colors are all on a number scale from 0, to 1. However, we get this number as a final result from a maximum color range of 255. The reason we get white from Color3.new(1,1,1) is actually because 255/255 = 1, and we're basically just saying that 3 times for R,G and B. Which is also why this would make a white color:
print(Color3.new(255/255,255/255,255/255))
But this shouldn't be any surprise to us, since anything divided by itself will return 1. But by using 255, we're going through every color variation possible for that spectrum. So, when i said something like _Color.R - i / 255, i was actually saying:
Subtract the old color property from the color value of i / 255 (which will return the absolute color value for that spectrum, and subtract it by the old one)
The final result will decrease the white color, by about a 3rd of all possible colors. (Making the new Color3 look like: 0.392, 0.392, 0.392)
So basically, you change these colors by changing each spectrum individually with a new Color3.new(). Hope this somewhat helped, I'm not sure how well i explained this... But, let me know if you have any questions.