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

[SOLVED] Converting strings into Color3?

Asked by 4 years ago
Edited 4 years ago

Hi everybody,

I'm making a system where you enter in a Color3 value (one in each box) and all the lights around you tween to the colour that was put in the textboxes. The obvious issue is each value is a string and not a number which therefore can't be used in a Color3Value. What would be the best way to convert a string into a Color3 that can be used later, I have a bit of code so you can see what I'm getting at (I'm probably going in the wrong direction

local AdvancedMode = script.Parent
local BasicMode = AdvancedMode.Parent.BasicMode
local modetoggle = AdvancedMode.Parent.modetoggle
local confirmbtn = AdvancedMode.confirmbutton
local inputR = AdvancedMode.inputR
local inputG = AdvancedMode.inputG
local inputB = AdvancedMode.inputB
local CurrentColour = AdvancedMode.CurrentColour

confirmbtn.MouseButton1Click:Connect(function()
    print('test')
    local inputR_numb = inputR.Text
    local inputG_numb = inputG.Text
    local inputB_numb = inputB.Text
    print(inputR_numb,inputG_numb,inputB_numb)
    local combo = inputR_numb..','..inputG_numb..','..inputG_numb
    --combo = tonumber(combo) tried, doesn't work.
    CurrentColour.Value = combo
end)

The error I get when I run this (with tonumber bit) invalid argument #3 (Color3 expected, got nil) The error I get without the tonumber bit invalid argument #3 (Color3 expected, got string) Same errors but at least I know I'm probably on the right track with tonumber. That's pretty much everything

0
Have you tried Color3.new(inputR.Text,inputG.Text,inputB.Text)? (and with tonumber aswell) Qariter 110 — 4y
0
I would also check to see if the input is a number with the inbuilt function tonumber() this function returns nill if you input letters. It should also work with float values too!.... EG print(tonumber("0123")) -- 123 Print(tonumber("0.123")) -- 0.123 print(tonumber("hello") -- nil TGazza 1336 — 4y
0
Problem solved, pretty sure I created a new color3 and ran math.ceil on each value VoidKeyword 111 — 4y

1 answer

Log in to vote
0
Answered by
rstkdev 15
4 years ago
Edited 4 years ago

you can use tonumber()

but there is some problems if you only use tonumber()

  1. if the player use any invalid character, there is gonna be an error
  2. if the player use a comma instead of a dot, there is gonna be an error and the player will be confused
  3. if the number is higher than 255 or lower than 0, it will error

so you can use this function to convert 3 strings in a color3 value

local function toColor3(r,g,b) -- takes 3 strings for args
    r = r:gsub(",","%.",1)
    g = g:gsub(",","%.",1)
    b = b:gsub(",","%.",1)

    r = tonumber(r); g = tonumber(g); b = tonumber(b)

    if r == nil or g == nil or b == nil then return false end

    return Color3.fromRGB(
        math.clamp(r,0,255),
        math.clamp(g,0,255),
        math.clamp(b,0,255)
    )
end

-- the function return the Color3 value or false if the strings are not valid

(btw i would use values from 0 to 255 instead of 0 to 1)

Ad

Answer this question