Brickcolor value not working

pcall the random color works but the team color doesnt, its returning a valid colorcode but the colorcode isnt working to change the color of the team, why is that (explination not spoonfed code pls)

local success, result = pcall(function ()
  local ColorMessage = string.match(group.Description, "Color:%d+,%d+,%d+") -- gets Color:R,G,B from group description
  local ColorSplitMessage = string.split(ColorMessage, ":") -- splits 
  local ColorText, Color = ColorSplitMessage[1], ColorSplitMessage[2] -- spliits into text and value
  print("team color changing to", Color) -- prints r,g,b
  team.TeamColor = BrickColor.new(Color) -- sets color (doesnt work? returns gray?)
end)
if not success then
  team.TeamColor = BrickColor.new(math.random(), math.random(), math.random()) -- works fine
end
1 Like

The issue is that BrickColor.new(Color) is not receiving three values corresponding to RGB components, it’s receiving a string of those numbers written out, and it doesn’t know what to do with them. It is interesting that it simply produces gray instead of throwing and error, but, basically, you need to pull the RGB values out of the string and convert them into numbers like this:

local success, result = pcall(function ()
	local ColorMessage = string.match(group.Description, "Color:%d+,%d+,%d+")	--Gets Color:R,G,B from group description.
	local R,G,B = string.match(ColorMessage,"(%d+),(%d+),(%d+)")				--Pulls each number from the string as text.
	R = tonumber(R)/255		--Converts "R" into its corresponding red channel component.
	G = tonumber(G)/255		--Converts "G" into its corresponding green channel component.
	B = tonumber(B)/255		--Converts "B" into its corresponding blue channel component.
	
	team.TeamColor = BrickColor.new(R,G,B)	--Creates the closest BrickColor to the specified RGB values.
end)
if not success then
	team.TeamColor = BrickColor.new(math.random(), math.random(), math.random())
end
2 Likes

Looks like you may want Color3.new() or Color3.fromRGB() instead of using BrickColor

That’s an intentional convention of the BrickColor class constructor function, instead of erroring (when an attempt is made to instantiate an invalid BrickColor value) it will product a grey, specifically a “Medium stone grey” BrickColor value.