t = table to save
filename = name of file to save the data to
location = folder to save the file to (optional, defaults to system.DocumentsDirectory)
I suspect that @agramonte simply left the “local” off of the myTable variable.
If you’re having trouble understanding global vs. local, getting a lot of nil variables that you think shouldn’t be or things behaving weirdly there is a good chance you need to understand “scope” better. This tutorial explains it: http://docs.coronalabs.com/tutorial/basics/scope/index.html
It’s really a simple concept once you understand what defines a block of code. It really helps if you properly indent your code so you can easily see each block of code.
Consider:
local myFirstVariable = 1 local function myFunction( ) local mySecondVariable = 2 if mySecondVariable == 2 then local myThirdVariable = 3 print( mySecondVariable ) -- prints 2 end print( myThirdVariable ) -- prints nil end
vs
local myFirstVariable = 1 local function myFunction( ) local mySecondVariable = 2 if mySecondVariable == 2 then local myThirdVariable = 3 print( mySecondVariable ) -- prints 2 end print( myThirdVariable ) -- prints nil end
The blocks of code are identical other than some formatting, but the first block is really hard to understand where one block of code begins and ends. The indentions should show you that there actually three blocks of code. One is a “main chunk” (code lined up against the left edge, a function block named myFunction() and a “if” block.
local variables can only be seen in the block they are defined and any children blocks, for instance, the “if” block is a child of the myFunction() block. The myFunction() block is a child of the main chunk.
This means that myThirdVariable only exists and can be seen inside the “if” block. The mySecondVariable only exists inside the function and can only be seen there. But since the “if” block is a child of the function, it can also see mySecondVariable. One the “if” block is done, myThirdVariable goes away and isn’t available to the rest of myFunction().
The indenting of the code really helps you visualize what’s going on.
Rob