Color3 pick from a table not working?

so i have a table which the color3 for the gui takes from but it is not working, what did i do wrong?

local Variables = {
	"0.986007, 0, 0.0538186",
	"0.133196, 0.328206, 0.998352",
	"0.16817, 0.789593, 0.182605",
	"0.985275, 0, 0.720073",
	"0.99765, 0.875898, 0.0387579",
	"0.988525, 0.358038, 0.0314183",
	"0.48365, 0, 0.998444",
}



gui.TextColor3 = Color3.new(Variables[math.random(1,#Variables)])

This will not work because the Color3.new function requires 3 numeric parameters, not one string. In order to achieve this, using the table you have provided, feel free to use the added function below:

local Variables = {
	"0.986007, 0, 0.0538186",
	"0.133196, 0.328206, 0.998352",
	"0.16817, 0.789593, 0.182605",
	"0.985275, 0, 0.720073",
	"0.99765, 0.875898, 0.0387579",
	"0.988525, 0.358038, 0.0314183",
	"0.48365, 0, 0.998444",
}

local function convert(str)
    local spl = string.split(str, ", ")
    for i,c in pairs(spl) do spl[i] = tonumber(c) end
    return table.unpack(spl)
end

gui.TextColor3 = Color3.new(convert(Variables[math.random(1,#Variables)]))
1 Like

hmmm i don’t think its working, theres an error saying

Players.foxnoobkite.PlayerGui.Cooldown.Frame.Shoot.Cooldown.LocalScript:20: attempt to index function with '0.133196, 0.328206, 0.998352'

maybe i have to just get the whole color3.new function in the table too?

1 Like

Yeah, I made a mistake with my syntax. I made an edit.

hmm nope, still the same black letter

local Variables = {
	"0.986007, 0, 0.0538186",
	"0.133196, 0.328206, 0.998352",
	"0.16817, 0.789593, 0.182605",
	"0.985275, 0, 0.720073",
	"0.99765, 0.875898, 0.0387579",
	"0.988525, 0.358038, 0.0314183",
	"0.48365, 0, 0.998444",
}



gui.TextColor3 = Color3.new(unpack(Variables[math.random(#Variables)]:split(", ")))

You could probably just store the values as Color3 instead of a string.

Their code works fine. Maybe you don’t have the gui variable set to the UI instance you are trying to change the color of?

That wouldn’t work because the parameters are string, hence why I used a for loop in my function to convert the strings to numbers.

If the string can successfully be converted to a number (where tonumber doesn’t return nil), it is counted as a number.

You are correct, my apologies.

Just so you’re aware a color channel to 2 decimal places is essentially the same as a color channel to 6 decimal places (at least in respect to the naked eye).

This is the best way to handle this.

local Variables = {
    Color3.new(0.986007, 0, 0.0538186),
    Color3.new(0.133196, 0.328206, 0.998352),
    Color3.new(0.16817, 0.789593, 0.182605),
    Color3.new(0.985275, 0, 0.720073),
    Color3.new(0.99765, 0.875898, 0.0387579),
    Color3.new(0.988525, 0.358038, 0.0314183),
    Color3.new(0.48365, 0, 0.998444),
}



gui.TextColor3 = Variables[math.random(1,#Variables)]
2 Likes