Need help getting a string to a Color3

I am trying to turn a string “#,#,#” (like “25,122,0”) to a Color3. How can I do this? I tried using Color3.fromRGB(string), but it just returned 0, 0, 0. What would be a way to do this?

2 Likes

You can split the string using string.split() or just myString:split(), and create a new Color3 value from there.

local myString = "25, 25, 25"
local splitted = string.split(myString, ", ") --splitting at every comma and space in myString

--Lua's index starts at 1 instead of 0
local colour = Color3.fromRGB(tonumber(splitted[1]), tonumber(splitted[2]), tonumber(splitted[3]))
4 Likes

you can also do

local ColorString = "25, 25, 25"
local R, G, B = string.match(ColorString, "(%d+), (%d+), (%d+)")

local Color = Color3.fromRGB(R, G, B)
3 Likes
local function StringToColor3(str: string)
	return Color3.fromRGB(string.match(str, "(%d+).+(%d+).+(%d+)"))
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.