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

how to Convert a string value to a color3 value?

{{'sbhcolor1'},{'Changes Scoreboards Home Color 1'},2,{Color3.fromRGB},function(pl,args) workspace.Scoreboard.Information.HomeIndicator1.Value = Color3.fromRGB(tostring(args[2])) end},

this is the script which is located on custom commands of kohls admin infinite, i want to say for example ":sbhcolor1 255,255,255" and make it happen but i dont seem to get it. can someone help?

3 comments

0

Use the tonumber() typecast function.

0

^ To convert a string that has commas in it to a number?

0

I believe what you're looking for is pattern matching; just use find() with sub().

1 answer

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.

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