TextColor3 random colors

Greetings everyone!

Recently I’ve been creating a new Menu GUI for my game and i want that the name of the player to change colors every time he/she opens the menu.
What i mean by that is :
https://gyazo.com/da0e8eb2bc928f13f1c6ae890a90801d
The name of the player should change every time he opens the menu

Here is the script :

--Defining Things
local OpenButton = script.Parent.OpenMenu
local CloseButton = script.Parent.CloseMenu
local Menu = script.Parent
local IsOpen = false
local NameText = script.Parent.Name

local Colors = {
				Color3.new(255,255,255),
				Color3.new(0,85,0),
				Color3.new(170, 255, 0),
				Color3.new(170, 85, 127)
				}

Menu.Position = UDim2.new(-0.208, 0,-0.163, 0) --Start Position

OpenButton.MouseButton1Click:Connect(function()
    if IsOpen == false then
         Menu:TweenPosition(
		UDim2.new(0, 0,-0.163, 0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		1.3,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
	   
       IsOpen = true
		NameText.TextColor3 = Color3.new(math.random(Colors))
	end
	
end)

CloseButton.MouseButton1Click:Connect(function()
	if IsOpen == true then
		  Menu:TweenPosition(
		UDim2.new(-0.208, 0,-0.163, 0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		1,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
       IsOpen = false
	end
end)

I tried in many other ways to fix this but none of them worked.

Thank you everyone for helping :heart:

The immediate problem I see is you’re creating the color wrong. Using Color3.new takes in a rgb but with all values from 0 to 1. Change them to Color3.fromRGB and see if that fixes it. fromRGB takes rgb from 0 to 255. You’re also instantiating a new color with a random value taking in a table? I think you mean to do

 NameText.TextColor3 = Colors[math.random(#Colors)];

instead.

1 Like

I think i fixed what i did wrong. However it’s still not working
This is the fixed version

local Colors = {
				Color3.fromRGB(255,255,255),
				Color3.fromRGB(0,85,0),
				Color3.fromRGB(170, 255, 0),
				Color3.fromRGB(170, 85, 127)
				}

Menu.Position = UDim2.new(-0.208, 0,-0.163, 0) --Start Position

OpenButton.MouseButton1Click:Connect(function()
    if IsOpen == false then
         Menu:TweenPosition(
		UDim2.new(0, 0,-0.163, 0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		1.3,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
	   
       IsOpen = true
		 NameText.TextColor3 = Colors[math.random(#Colors)];
	end
	
end)

My bad if i don’t understand instantly. I am new to scripting so i might not know why or how everything works

It looks like you never defined IsOpen beforehand, before the function you have to do IsOpen = false.

local OpenButton = script.Parent.OpenMenu
local CloseButton = script.Parent.CloseMenu
local Menu = script.Parent
local IsOpen = false
local NameText = script.Parent.Name

local Colors = {
				Color3.fromRGB(255,255,255),
				Color3.fromRGB(0,85,0),
				Color3.fromRGB(170, 255, 0),
				Color3.fromRGB(170, 85, 127)
				}

i defined IsOpen above. I also tried moving it before the function starts and got the same results

You see the variable “local NameText = script.Parent.Name”

change that to

local NameText = script.Parent.PlayerName

and then change the textbox to PlayerName - Atm this script is getting the name of the textbox parent and trying to change the color of the parent of the textbox as Name is a property of everything so your calling that property rather than the textbox called “Name”

In a different script i mentioned that.

script.Parent.Text = game.Players.LocalPlayer.Name

And I’ve also tried exactly what you said. Sadly i got the same results.
This is how it looks in studio image
so im sure the name script works.

Get a random Color3 Value like this

 local color = Color3.new(math.random(), math.random(), math.random()) 
 print(color)
 Frame.BackgroundColor3 = color -- random color

math.random() with no arguments returns a random number 0<x<1,
and Color3.new() accepts 3 values x/255.

Edit: in your case,

NameText.TextColor3 = Color3.new(math.random(Colors))

is incorrect, as math.random() doesn’t take tables as arguments,
you meant to do this

NameText.TextColor3 = Colors[math.random(#Colors)]

I do not want a totally random color, which is why i created a table of a few test colors to see if the script works. However , every time i open the menu i get this error

 [Players.Reinellex.PlayerGui.NewMenu.MainFrame.Open/Close Menu:25: attempt to get length of a userdata value]

Which is related to this

NameText.TextColor3 = Color3.new(Colors[math.random(#Colors)])

You edited the post and i didn’t noticed. My bad!
Now i corrected my mistake and i get the same error.

local OpenButton = script.Parent.OpenMenu
local CloseButton = script.Parent.CloseMenu
local Menu = script.Parent
local NameText = script.Parent.Name

local Colors = Color3.new(math.random(), math.random(), math.random()) 
 print(Colors)
Menu.Position = UDim2.new(-0.208, 0,-0.163, 0) --Start Position

IsOpen = false
OpenButton.MouseButton1Click:Connect(function()
    if IsOpen == false then
         Menu:TweenPosition(
		UDim2.new(0, 0,-0.163, 0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		1.3,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
	   
       IsOpen = true
		NameText.TextColor3 = Colors[math.random(#Colors)]
	end
	
end)

I am so sorry for not understanding.

Also the Colors are printed and are, as you said, numbers between 0 and 1

1 Like

And now Colors isn’t a table, doing

Colors[math.random(#Colors)]

won’t work.

Just change

  NameText.TextColor3 = Colors[math.random(#Colors)]

to this

  NameText.TextColor3 = Colors -- technically one color though
Players.Reinellex.PlayerGui.NewMenu.MainFrame.Open/Close Menu:25: attempt to index string with 'TextColor3'

New error :frowning:

--Defining Things
local OpenButton = script.Parent.OpenMenu
local CloseButton = script.Parent.CloseMenu
local Menu = script.Parent
local NameText = script.Parent.Name

local Colors = Color3.new(math.random(), math.random(), math.random()) 
 print(Colors)
Menu.Position = UDim2.new(-0.208, 0,-0.163, 0) --Start Position

IsOpen = false
OpenButton.MouseButton1Click:Connect(function()
    if IsOpen == false then
         Menu:TweenPosition(
		UDim2.new(0, 0,-0.163, 0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		1.3,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
	   
       IsOpen = true
		 NameText.TextColor3 = Colors
	end
	
end)

CloseButton.MouseButton1Click:Connect(function()
	if IsOpen == true then
		  Menu:TweenPosition(
		UDim2.new(-0.208, 0,-0.163, 0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		1,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
       IsOpen = false
	end
end)

You are indexing the name of the TextLabel. Remove the .Name in the NameText variable

Oh! I just realised that the name of the TextLabel was the same with the Property name . I am so so sorry for this.
I’ve also fixed the random colors by replacing local Colors inside the function. Thank you E44 and ELECTRO for helping :heart: :heart:

2 Likes