Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to turn a String into a Color3 Value?

How can I turn a string into a Color3 value? The string is "255,50,52" and if I have that as variable c then Color3.fromRGB(c) says Color3 expected, got string. If I do tonumber(c) it gets nil.

local c = "255,50,52"
local color = Color3.fromRGB(c)
-- Output = "Color3 expected, got string"

,

local c = "255,50,52"
local color = Color3.fromRGB(tonumber(c))
-- Output = "Color3 expected, got nil"

2 comments

0

%d+, %d+, %d+

0

not sure why you want to do that when you can just seperate your 3 values into 3 different arguments like Color3.fromRGB(255, 50, 52)

1 answer

✓ Accepted answer
function color3(r,g,b)
	
	if (typeof(r) == 'string' and tonumber(r) == nil) then --check if you're inputting a string
		local t = {}; --create a table
		for i in r:gmatch('(%d*)') do --get all numbers by using a string pattern
			t[#t+1] = tonumber(i) --put them in the table
		end; 
		if (g == true) then --if a bool is inputted. i don't use if (g) then, because if g is a string then it will also pass the if statement
			return Color3.fromRGB((t[1] or 0),(t[2] or 0),(t[3] or 0))
		end
		return t; --return the table
	end
	
	return Color3.fromRGB(tonumber(r),tonumber(g),tonumber(b)); --otherwise, return the rgb
end

yeah here you go so the comments basically tells you what it is this function is basically useless and you should never ever ever use it i guess but yeah here you go, this should help your cause ig. idk why you would ever want to use a string as a color3 value but you do you

-- all you have to do is a simple

local aregeebee_Table = color3('255, 255 255') --returns table
local aregeebee_c3 = color3('255, 255, 255', true) --returns a color3.New value for some odd reason idk but yeah it works

This discussion is preserved as it was. New answers, comments, and votes are closed.