Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to turn a String into a Color3 Value?

Asked by
xJigoku 17
5 years ago

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"
0
%d+, %d+, %d+ User#24403 69 — 5y
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) GoldAngelInDisguise 297 — 5y

1 answer

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago
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
Ad

Answer this question