How can I tween the 'Color' porperty of a UI Gradient?

Hi there. Been making recently some UIs until I ran into a problem. So I made a frame and added the ‘UI Gradient’ object to it so that the color of the frame can be, well, a gradient. I set the color sequence to a dark green at position 0 and a slightly lighter green at position 1. Now I wanted to make it so that once a button is clicked, that the Dark Green-Light Green gradient transforms into a Dark Red-Light Red gradient. So far no problems yet, I used the following code to change the colorsequence on the UI Gradient:

Location.UIGradient.Color = ColorSequence.new { ColorSequenceKeypoint.new(0, DarkRed), ColorSequenceKeypoint.new(1, LightRed) }

After I tested this, and found out it was working perfectly, I wanted to do a slight change to the script so that the transition from one color sequence to another is tweened, making it look way more smooth. So I edited the script to this:

game:GetService('TweenService'):Create(Location.UIGradient,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {Color = ColorSequence.new { ColorSequenceKeypoint.new(0, DarkRed), ColorSequenceKeypoint.new(1, LightRed) }}):Play()

And this is where I got the following error: “TweenService:Create property named ‘Color’ on object ‘UIGradient’ is not a data type that can be tweened”.

My question here is:

  1. Is it possible to somehow make a smooth color transtion on UI Gradients using TweenService?
  2. And if yes, how could I achieve it?

Thank you for your attention. I’m looking forward to any type of help.

You wouldn’t be able to use the TweenService for this. You could instead just lerp the colors and update your UIGradient color each frame until you have achieved the color you’re seeking.

Color3 | Roblox Creator Documentation has a :lerp(goal_color: Color3, alpha: number) function you could use. You can also mock up certain EasingStyles using the TweenService | Roblox Creator Documentation API.

3 Likes

Alrighty thank you! I’m gonna try this and see if it works :wink: