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!

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.

it is a txt file that only contains “3000”

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

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.

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.

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!

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