Color3 is not setting to value

"[^,]+" this search pattern doesn’t account for whitespaces, fortunately the ‘Color3’ class constructor functions trim leading/trailing whitespaces.

print(Color3.new(" 1 ", " 0.5 ", " 0.25 ")) --1, 0.5, 0.25

To match one or more occurences of numeric digits simply use the following.

for colorChannel in string.gmatch(colorString, "%d+") do
	--Do code.
end

Well correct “%d+” matches one or more occurences of numeric digits. But “%d+” doesn’t take float numbers into account and not that he uses or will use one, but your code returns an invalid result if he does.

Tested code

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

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


local colorString = "255, 83.75, 20.1"
print("Expected Color ->", Color3.fromRGB(255, 83.75, 20.1))
print("match([^,]+) --->", colorFromString1(colorString))
print("match(%d+) ----->", colorFromString2(colorString))

Output:
image

Lua console
image

image

%d+%.?%d*
For matching floats.

local s = string.match(math.pi, "%d+%.?%d*")
print(s) --3.14...

No leading/trailing whitespaces (or other characters).

Yes, but there’s no problem with leading/trailing whitespaces in the tonumber, so why make an ugly pattern, the one above is cleaner and does the job. If whitespace is a problem just add a space in the pattern like this “[^, ]+” way cleaner.
But thanks for the sujestion.

so why make an ugly pattern

Because it matches exactly what you’re trying to match and nothing else.

this “[^, ]+” way cleaner

If you’re going to do this you should use the whitespace character class instead (handles every type of whitespace character) %s.

Good sujestion “[^,%s]+” didn’t think about the other whitespaces not that he will ever use \n or \t but it’s good to always be safe.