How could I convert this "string" to a number variable type?

function getcolor(colorstring)
    colorstring = idk how convert to number :(
end

local test = "[255,255,255]"
getcolor(test)
wait()
print(test)

example with tonumber ()
image

EDIT:

local function getcolor(colorstring)
    local a = colorstring:split("[")
    local b = a[2]:split("]")
    local c = b[1]:sub(1, -1):split(",")
    print(Color3.fromRGB(unpack(c))) -- print in color3.new
end

local test = "[255,255,255]"
getcolor(test)

Thanks rogchamp, I made this based on yours
//He won’t let me reply to your message (ERROR 403)

Try using string manipulation to grab just the numbers you need, and then use them to make a Color3:

local function getcolor(colorstring)
	colorstring = colorstring:sub(2, -2) -- get rid of the brackets
	local colors = colorstring:split(",") -- split it into an array by commas
	return Color3.fromRGB(unpack(colors)) -- put the stuff in the table as color3 parameters
end

4 Likes

@S_xnw you don’t need to make the IntValues a StringValue. I’m sure you know this but I will go over it again.
BooleanValue = true/false
StringValue = “insert text here”
IntegerValue = insert whole number here
FloatValue = insert number with decimal here

I tested the original code in a baseplate, and the result returned nil. It appears this is why:

The variable “test” has special characters that affect the result(s).
Also, noticed the result wasn’t returned.

Information about tonumber() can be found here and here. Hope this helps and good luck!

Here is some examples:

-- Example 1
function getcolor(colorstring)
    colorstring = tonumber(colorstring)
    return colorstring -- After the conversion make sure to return the result
end

local example = "[255, 255, 255]"
local converted = getcolor(example) -- We call the function as a variable to get the result
print(converted) -- This will print nil in the output because of the special characters and spaces
-- Example 2
function getcolor(colorstring)
    colorstring = tonumber(colorstring)
    return colorstring -- After the conversion make sure to return the result
end

local example = "255"
local converted = getcolor(example) -- We call the function as a variable to get the result
print(converted) -- This will print the number (if there is no errors)