Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Code only works in Air Coding mode

For some reason this line is giving me an error when trying to run from my device, but works fine when I am using Air Code.

local goal1 = math.tointeger(readText(asset.documents.g1))

the error is: bad argument #1 to 'tointeger' value expected

Please help!

Tagged:

Comments

  • dave1707dave1707 Mod
    edited August 2020 Posts: 8,968

    What is g1 supposed to be. tointeger will only convert text digits. If there’s a decimal point or alpha characters, it returns nil. It also returns nil if there too many digits.

    PS. I’ve never used Air Code so I can’t comment why it works there.

  • Posts: 8

    it is a txt file that only contains "3000"

  • dave1707dave1707 Mod
    Posts: 8,968

    I created a text file named g1 with the value of 3000 in it. This ran OK and printed 3000. Do you have 3000 in “ “ or just 3000 and nothing else.

    displayMode(STANDARD)
    
    function setup()    
        local goal1 = math.tointeger(readText(asset.documents.g1))  
        print(goal1)  
    end
    
  • Posts: 8

    hmm I just ran that code with my g1, and it worked. I think my issue is that the line in my other code is not inside of any function block. Do you know how I could fix this? I would like to reference it from another function later.

  • dave1707dave1707 Mod
    Posts: 8,968

    Don’t make goal1 local. If a variable is local it can only be accessed from the function it’s in. If it not local (global) it can be accessed anywhere.

  • Posts: 8

    got it thanks! everything works now. Yea the global solution was pretty obvious... I kept putting global in front of my variables (whoops)... Thanks so much for the help!

  • dave1707dave1707 Mod
    Posts: 8,968

    That’s what I’m here for. Glad it works. If you have any other questions, just ask.

Sign In or Register to comment.