@haillin's answer fades the part out (transparent) and then changes the color, which I don't think is the question you are asking?
Unfortunately, ROBLOX doesn't allow you to have continuous colors for the BrickColor of a Part. Only the set palette of BrickColors can be used.
Luckily, they do allow this to be done with SpecialMesh's.
Here is how you could make the part fade, which you could do on Touched
. Most likely, you should remember to make some sort of debounce system so it will not start fading again until it finishes. This snippet includes that.
I assume the Script is parented to the Part in question, and that it has a SpecialMesh called Mesh using the following mesh:
http://www.roblox.com/Mini-Brick-item?id=9857022
and with a Texture of solid white (just search through free decals).
03 | script.Parent.Mesh.Scale = script.Parent.Size * 2 ; |
12 | local black = Vector 3. new( 0 , 0 , 0 ); |
13 | local red = Vector 3. new( 0 , 0 , 1 ); |
17 | local col = black * ( 1 -i) + red * i; |
23 | script.Parent.Mesh.VertexColor = col; |
This solution has other limitations, unfortunately. If you use a mesh, you won't be able to see the material of a part, or any of its surfaces. It also won't have outlines.
EDIT: A more modular approach
Thinking on this, there's a much cleaner way to manage fading. We can make ourselves a fade
function which lets us use this in more contexts than just the touched event in a clean way.
01 | function fade(part,from,to,time) |
03 | for i = 0 , 1 , 1 /(time/. 03 ) do |
04 | local col = from * ( 1 -i) + to * i; |
05 | part.Mesh.VertexColor = col; |
18 | fade(script.Parent, Vector 3. new( 0 , 0 , 0 ), Vector 3. new( 1 , 0 , 0 )); |
21 | script.Parent.Touched:connect(touched); |