Color3 is not setting to value

Hi! I have this script in datastore that when the specified remote event is triggered, the style is either bought or equipped. The buying part is fine but the problem is the equipping part, specifically setting the player’s nametag to a specific color. Here is my script:

StyleSelectedRE.OnServerEvent:Connect(function(plr, style)
	if not style or typeof(style) ~= "Instance" then return end -- we still want this here just in case

	local ownedStyle = plr.OwnedStyles:FindFirstChild(style.Name)




	if not ownedStyle then
		print(plr.Name," is buying the ",style.Name," style.")

		local price = style.Price.Value
		local coins = plr.leaderstats.Coins

		if price <= coins.Value then
			coins.Value -= price

			local newTool = style:Clone() do
				newTool.Parent = plr.OwnedStyles



			end
		end
	elseif ownedStyle then
		print("test")
		print(plr.Character.Head.NameTag.Title.TextColor3)
		print(style.MainColor.Value)
		if plr.Character.Head.NameTag.Title.TextColor3 ~= style.MainColor.Value then

			print(plr.Name," equipped ",style.Name," tool.")

			plr.Character.Head.NameTag.Title.TextColor3 = Color3.new(style.MainColor.Value)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = Color3.new(style.Secondary.Value)
			plr.Character.Head.NameTag.PlayerName.TextColor3 = Color3.new(style.MainColor.Value)
			plr.Character.Head.NameTag.PlayerName.TextStrokeColor3 = Color3.new(style.Secondary.Value)
		elseif plr.Character.Head.NameTag.Title.TextColor3 == style.MainColor.Value then
			print("triggered")

			plr.Character.Head.NameTag.Title.TextColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Name.TextColor3 = Color3.fromRGB(255, 255, 255)
			plr.Character.Head.NameTag.Name.TextStrokeColor3 = Color3.fromRGB(255, 255, 255)

		end
	end
end)

	

Instead of setting it to the rgb value of style.MainColor.Value, it just sets it to black. I added a print statement to try to debug it above the equip message:

print(style.MainColor.Value)

But the correct rgb numbers are showing but the color’s nametag is still black. What do I have to do?

what value type is

style.MainColor.Value

String, I can’t add commas to int values

well then you have to parse the string to 3 numbers first because Color3.new() only accepts numbers I think

use this function to convert your string example: “0, 1, 0” → Color3.new(0, 1, 0)

local function colorFromString(colorStr: string): Color3
      local components = {}
      for component in colorStr:gmatch("[^,]") do
          table.insert(components, tonumber(component))
      end
      return Color3.new(table.unpack(components))
end

usage

local color = colorFromString(style.MainColor.Value)

or you can use Color3Values instead of StringValues

What would I have to change for Style.Maincolor.Value for this to work, and what do I need to change from the colorFromString function to fit my script?

I will post it in a sec but a quick question your string format is somthing like this right? “R, G, B”

Yep. Thats how I format it the value.

local function colorFromString(colorStr: string): Color3
      local components = {}
      for component in colorStr:gmatch("[^,]") do
          table.insert(components, tonumber(component))
      end
      return Color3.new(table.unpack(components))
end

StyleSelectedRE.OnServerEvent:Connect(function(plr, style)
	if not style or typeof(style) ~= "Instance" then return end -- we still want this here just in case

	local ownedStyle = plr.OwnedStyles:FindFirstChild(style.Name)




	if not ownedStyle then
		print(plr.Name," is buying the ",style.Name," style.")

		local price = style.Price.Value
		local coins = plr.leaderstats.Coins

		if price <= coins.Value then
			coins.Value -= price

			local newTool = style:Clone() do
				newTool.Parent = plr.OwnedStyles



			end
		end
	elseif ownedStyle then
		print("test")
		print(plr.Character.Head.NameTag.Title.TextColor3)
		print(style.MainColor.Value)
		if plr.Character.Head.NameTag.Title.TextColor3 ~= colorFromString(style.MainColor.Value) then

			print(plr.Name," equipped ",style.Name," tool.")

			plr.Character.Head.NameTag.Title.TextColor3 = colorFromString(style.MainColor.Value)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = colorFromString(style.Secondary.Value)
			plr.Character.Head.NameTag.PlayerName.TextColor3 = colorFromString(style.MainColor.Value)
			plr.Character.Head.NameTag.PlayerName.TextStrokeColor3 = colorFromString(style.Secondary.Value)
		elseif plr.Character.Head.NameTag.Title.TextColor3 == colorFromString(style.MainColor.Value) then
			print("triggered")

			plr.Character.Head.NameTag.Title.TextColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Name.TextColor3 = Color3.fromRGB(255, 255, 255)
			plr.Character.Head.NameTag.Name.TextStrokeColor3 = Color3.fromRGB(255, 255, 255)

		end
	end
end)

check if this works

Forgot to ask, did you use the 0 - 255 range or 0 - 1 for each component

I used the 0-255 range for each value.

Ok than I have to change it a little bit.

local function colorFromString(colorStr: string): Color3
      local components = {}
      for component in colorStr:gmatch("[^,]") do
          table.insert(components, tonumber(component))
      end
      return Color3.fromRGB(table.unpack(components))
end

StyleSelectedRE.OnServerEvent:Connect(function(plr, style)
	if not style or typeof(style) ~= "Instance" then return end -- we still want this here just in case

	local ownedStyle = plr.OwnedStyles:FindFirstChild(style.Name)




	if not ownedStyle then
		print(plr.Name," is buying the ",style.Name," style.")

		local price = style.Price.Value
		local coins = plr.leaderstats.Coins

		if price <= coins.Value then
			coins.Value -= price

			local newTool = style:Clone() do
				newTool.Parent = plr.OwnedStyles



			end
		end
	elseif ownedStyle then
		print("test")
		print(plr.Character.Head.NameTag.Title.TextColor3)
		print(style.MainColor.Value)
		if plr.Character.Head.NameTag.Title.TextColor3 ~= colorFromString(style.MainColor.Value) then

			print(plr.Name," equipped ",style.Name," tool.")

			plr.Character.Head.NameTag.Title.TextColor3 = colorFromString(style.MainColor.Value)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = colorFromString(style.Secondary.Value)
			plr.Character.Head.NameTag.PlayerName.TextColor3 = colorFromString(style.MainColor.Value)
			plr.Character.Head.NameTag.PlayerName.TextStrokeColor3 = colorFromString(style.Secondary.Value)
		elseif plr.Character.Head.NameTag.Title.TextColor3 == colorFromString(style.MainColor.Value) then
			print("triggered")

			plr.Character.Head.NameTag.Title.TextColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Name.TextColor3 = Color3.fromRGB(255, 255, 255)
			plr.Character.Head.NameTag.Name.TextStrokeColor3 = Color3.fromRGB(255, 255, 255)

		end
	end
end)

Is this the script I need to use? Its doing the same thing again where every part is black.

Well then I messed somthing up try this to check the value look in the console for “Input: R, G, B Ouput: R, G, B” and send a screenshot or the printed message

local function colorFromString(colorStr: string): Color3
      local components = {}
      for component in colorStr:gmatch("[^,]") do
          table.insert(components, tonumber(component))
      end
      return Color3.fromRGB(table.unpack(components))
end

StyleSelectedRE.OnServerEvent:Connect(function(plr, style)
	if not style or typeof(style) ~= "Instance" then return end -- we still want this here just in case

	local ownedStyle = plr.OwnedStyles:FindFirstChild(style.Name)




	if not ownedStyle then
		print(plr.Name," is buying the ",style.Name," style.")

		local price = style.Price.Value
		local coins = plr.leaderstats.Coins

		if price <= coins.Value then
			coins.Value -= price

			local newTool = style:Clone() do
				newTool.Parent = plr.OwnedStyles



			end
		end
	elseif ownedStyle then
		print("test")
		print(plr.Character.Head.NameTag.Title.TextColor3)
		print("Input:", style.MainColor.Value, "Output", colorFromString(style.MainColor.Value))
		if plr.Character.Head.NameTag.Title.TextColor3 ~= colorFromString(style.MainColor.Value) then

			print(plr.Name," equipped ",style.Name," tool.")

			plr.Character.Head.NameTag.Title.TextColor3 = colorFromString(style.MainColor.Value)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = colorFromString(style.Secondary.Value)
			plr.Character.Head.NameTag.PlayerName.TextColor3 = colorFromString(style.MainColor.Value)
			plr.Character.Head.NameTag.PlayerName.TextStrokeColor3 = colorFromString(style.Secondary.Value)
		elseif plr.Character.Head.NameTag.Title.TextColor3 == colorFromString(style.MainColor.Value) then
			print("triggered")

			plr.Character.Head.NameTag.Title.TextColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Title.TextStrokeColor3 = Color3.fromRGB(218, 218, 218)
			plr.Character.Head.NameTag.Name.TextColor3 = Color3.fromRGB(255, 255, 255)
			plr.Character.Head.NameTag.Name.TextStrokeColor3 = Color3.fromRGB(255, 255, 255)

		end
	end
end)

image

well its doing somthing as its not pure black but is still not correct

Replace this funciton

local function colorFromString(colorStr: string): Color3
      local components = {}
      for component in colorStr:gmatch("[^,]") do
          table.insert(components, tonumber(component))
      end
      print("ColorFromString input:", colorStr, "output", table.unpack(components))
      return Color3.fromRGB(table.unpack(components))
end

and again check the console for

Ok my bad forgot a + in the match sry.

local function colorFromString(colorStr: string): Color3
      local components = {}
      for component in colorStr:gmatch("[^,]+") do
          table.insert(components, tonumber(component))
      end
      print("ColorFromString input:", colorStr, "output", table.unpack(components))
      return Color3.fromRGB(table.unpack(components))
end

You can remove the print function if it works but keep it for now just to debug

1 Like

It worked. Thank you so much! :smiley:

1 Like