Using tonumber with strings from keyboardBuffer for a variable color

-- Tonumber

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    redColor = 255
  
end

function draw()

    background(40, 40, 50)
    fill(redColor,255,0,255)
        nbr=keyboardBuffer()
    if not nbr == nil then
        redColor=tonumber(nbr)
        end
    text("Hello World! WOLOLO",600,600)
    strokeWidth(5) 
    
end

function touched(touch)
    if touch.state == ENDED then
        showKeyboard()
    end
end

I’m trying to make the redColor variable accept a number value from the keyboard. Since keyboardBuffer only makes strings and fill only takes numbers I try to convert it with tonumber()

My code doesn’t work.

Try

if nbr~="" then

Thanks. Implemented your fix, added this to stop error when redColor was nil

if redColor == nil then 
    redColor= 255 
end 

@xThomas Here’s an example of using the keyboard function to get color values. You can key a value for red, green, or blue. Seperate the values with a comma, or just key a comma to accept a value of zero and press return. For example, keying 255 will give a color of red (255,0,0). Keying ,255 will give green (0,255,0) or ,255 will give blue (0,0,255). Keying 255,255,255 will give white (255,255,255) and 255,255 will give purple (255,0,255) and 255,255 will give yellow (255,255,0). Of course you can key any value from 0 to 255 and you can press the backspace key to correct an input error before pressing return.

function setup()
    showKeyboard()
    str=""
    red,green,blue=0,0,0
end

function draw()
    background(red,green,blue)
    fill(0)
    text(red..","..green..","..blue,WIDTH/2,HEIGHT-100)
end

function keyboard(k)
    if k==RETURN then
        r1,c,g1,c,b1=string.match(str,"(%d*)(,?)(%d*)(,?)(%d*)")
        red=tonumber(r1) or 0
        green=tonumber(g1) or 0
        blue=tonumber(b1) or 0
        str=""
    elseif k==BACKSPACE then
        str=string.sub(str,1,str:len()-1)
    else
        str=str..k
    end
    output.clear()
    print(str)
end

Thank you that was very helpful, I got to look up http://lua-users.org/wiki/PatternsTutorial and learned a whole bunch.

Also thanks to Codea makers because I very much use the feature where I tap next to code and it tells me what these things are called