Assuming the string is formatted either as this:
"255, 255, 255"
or this:
"255,255,255"
We can convert the string to a Color3 value.
local function toColor3(value)
local array = string.find(value, " ") and string.split(value, ", ") or string.split(value, ",")
local r = tonumber(array[1])
local g = tonumber(array[2])
local b = tonumber(array[3])
return Color3.fromRGB(r, g, b)
end
local color3Value = toColor3("56, 12, 174")
print(color3Value)
This function uses a ternary operation on its first line to determine whether or not the string contains spaces. The ternary operation returns an array containing each value in between each comma.
You can find more information about the split function here.
After retrieving an array containing each value, we can convert them to a number and assemble all of the values into a single Color3 value, and return it.
3 comments
Use the tonumber() typecast function.
^ To convert a string that has commas in it to a number?
I believe what you're looking for is pattern matching; just use find() with sub().