Tweening text to change into a random color

Hello DevForum!

So recently I was making a model keyboard, and it was going pretty standard, until I decided to label the keys.

Not so bad, but I unfortunately didn’t stop there, and as the first part of my name implies (Disco), I decided it would be a great idea to make it a gaming keyboard, where the text fades into different colors! (It was infact, not a great idea.)

Nonetheless I’m a bit stuck. There are multiple text labels that have to change colors, so I used a loop to specify all of them. I as well used color3 and math.random to generate a random color, of which said text labels will change its color to, which works fine.

Where I hit the roadblock is changing the colors smoothly (with tween service), often getting “Unable to cast to dictionary” errors.

while true do
	wait(0.1)
	
	
	local TweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(5)
	local color = Color3.new(math.random(), math.random(), math.random())
	
	

for _, v in pairs (script.Parent:GetDescendants())do
		if v.Name == 'TextLabel' then
			local tween = TweenService:Create(v, tweenInfo, color)
			wait()
			tween:play()
			
		end
	end
end

I’m not exactly the most experienced scripter, so sorry if this is an eyesore to look at.
How would I go about fixing this? Thanks!

1 Like

the third argument of a tween should be a table,

this line should look something like this:

local color = {TextColor3 = Color3.new(math.random(), math.random(), math.random())}

The issue occurs because of an incorrect argument type passed through the :Create() method of TweenService. The :Create() method consists of 3 parameters which are (Instance, TweenInfo, Dictionary). Where you went wrong was that you added a Color3 class value into the third argument which only supports a dictionary i.e. the property table.

Dictionaries can be written like this:

{Index = Value}

In this case though, it would be written like this:

{TextColor3 = color}

Now altogether, your code should look like this:

while true do
	wait(0.1)
	
	
	local TweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(5)
	local color = Color3.new(math.random(), math.random(), math.random())
	
	

for _, v in pairs (script.Parent:GetDescendants())do
		if v.Name == 'TextLabel' then
			local tween = TweenService:Create(v, tweenInfo, {TextColor3 = color})
			wait()
			tween:play()
			
		end
	end
end

To avoid further issues like this, it would be a good idea to look at the information given through the autocomplete in the Script Editor:
image
As you may see, it is clearly stated that the propertyTable/third parameter is a dictionary, aswell as some other helpful information.

Thank you to everyone who replied!! I managed to get it fixed!

Here’s the finished product, now all we need is a whole gaming PC, and we’re winning both Phantom Forces and Arsenal games alike!

(Text and light base do fade colors, however I dont have a good screen recording software to capture it with)

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