I want to write a button’s id to a txt file, but every time I run this function:
local function checkfile() if not file then file:write( saveData ) io.close( file ) else local contents = file:read("\*a") io.close(file) print(contents) end end
It always prints nil. This is my code so far:
local function writeFile(event) local button = event.target local saveData = button.id local path = system.pathForFile("file.txt", system.DocumentsDirectory) local file, errorString = io.open(path, "w") local function checkfile() if not file then file:write( saveData ) io.close( file ) else local contents = file:read("\*a") io.close(file) print(contents) end end if event.phase == "began" then if button.id == "pacifist" then text = "Good, mercy is the way to go." myText.text = text myText:setFillColor(0, 1, 1) checkfile() end if button.id == "genocide" then text = "You monster" myText.text = text myText:setFillColor(1, 0, 1) checkfile() end end end
I am very confused.