the following code is supposed to do this:
check for folder named “save”
if not, make folder, restart
if so, check for .txt file named “lastPlayed.txt” inside of folder
if so, read it
if not, create the .txt file, store os.time() inside of it
the only trouble i am having is when it checks to see if the file exists. It says it does exist, but when trying to read it, it figures out there is no .txt file. I am confused.
function startUp()
local saveFolder = lfs.chdir(system.pathForFile("save", system.DocumentsDirectory))
if saveFolder then
print("found save")
local lastPlayedtxtPath = system.pathForFile("save/lastPlayed.txt", system.DocumentsDirectory)
if lastPlayedtxtPath then
print("found lastPlayedtxt, reading...")
io.input(lastPlayedtxtPath)
print(io.read("*a"))
io.input():close()
else
print("no lastPlayedtxt")
local lastPlayedtxtPath = system.pathForFile("save/lastPlayed.txt", system.DocumentsDirectory)
local lastPlayedtxt, errorCode = io.open(lastPlayedtxtPath, "w+")
if lastPlayedtxt then
print("created new lastPlayedtxt")
lastPlayedtxt:write(tostring(os.time()))
io.close(lastPlayedtxt)
print("restarting")
startUp()
else
print("error: ".. errorCode)
end
end
else
print("no save")
local docsPath = system.pathForFile("", system.DocumentsDirectory)
local changeDir = lfs.chdir(docsPath)
local saveFolderPath
local saveFolderName = "save"
if changeDir then
lfs.mkdir(saveFolderName)
saveFolderPath = lfs.currentdir().."/"..saveFolderName
end
print("made save, restarting...")
startUp()
end
end
now, when run when the there is no save file, the console says this:
no save
made save, restarting…
found save
found lastPlayedtxt, reading…
ERROR: Runtime error
error is: Documents\save/lastPlayed.txt: No such file or directory
obviously there will be no .txt file if there was no save folder to begin with, so why does system.pathForFile return true?