How would I script a color-changing part using "onTouch"

Hey everyone!

Recently I was in the process of remaking my obby, I need some help with scripting a smooth transitioned color changing part using the “onTouch” function.

local ColorChange = script.Parent

function onTouched()
	wait(0.2)
	ColorChange.Touch:Connect(onTouched)
	ColorChange.Material = Enum.Material.Neon
	ColorChange.Color = Color3.fromRGB(6, 189, 255)
	wait(0.2)
	ColorChange.TouchEnded:Connect(onTouched)
	ColorChange.Material = Enum.Material.Plastic
	ColorChange.Color = Color3.fromRGB(0, 0, 0)
end

For some reason, this script doesn’t work, and the output log shows nothing. Whenever a user steps on this part, the black color should transition to light blue. However, when the user stops stepping on the part, it should then return to its original color and material. Any help is appreciated!

1 Like

You do call the function later in your script, and aren’t showing that right now, correct?

I think this should work, If you wanted to make it smooth you could use TweenService. Let me know if it works.

local ColorChange = script.Parent

function onTouched()
    wait(0.2)
    ColorChange.Touched:Connect(onTouched)
    ColorChange.Material = Enum.Material.Neon
    ColorChange.Color = Color3.fromRGB(6, 189, 255)
    wait(0.2)
    ColorChange.TouchEnded:Connect(onTouched)
    ColorChange.Material = Enum.Material.Plastic
    ColorChange.Color = Color3.fromRGB(0, 0, 0)
end

script.Parent.Touched:Connect(onTouched)

3 Likes

Looks good to me, I went ahead and beautified it for you! :sun_with_face:

2 Likes

Thanks for all your help! It works now. :+1:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.