title: Permission issues in lua
body: <p>Is it required to set some specific permissions in corona build.settings to save high score in file permanently?</p>
  
  <p>I'm getting and error every time I run the code saying "Permission denied"
  How to rectify this error?</p>
  
  <p>Here is the code I tried:</p>
  
  <pre><code>function read_score()
  
  local f1 = assert(io.open(path, "r+"))
  local contents = f1:read( "*a" )
  highScore = tonumber(contents)  
  if highScore==nil  
  then highScore=0
  
  
  elseif score&gt;highScore
      then
      highScore=score
  end
      f1:write(highScore)
      f1:close()
  disp_permScore()
  
  end
  
  
  function disp_permScore() --Function to display the high score
  local f1 = assert(io.open(path, "r"))
  local contents = f1:read( "*a" )
  highScore = tonumber(contents)  
  
      text_display2= display.newText(" BEST: " ..highScore, 0, 0, "Helvetica", 90)
      text_display2.x = centerX
      text_display2.y = centerY + 80
      text_display2.alpha=1 
  f2:close()
  end
  
  
  function gameOver()
  local f1 = assert(io.open(path, "r+"))
  local contents = f1:read( "*a" )
  highScore = tonumber(contents)  
  
  if score&lt;highScore
      then
      disp_permScore()
      else
      read_score()
  end
  </code></pre>
  
  <p>Please tell where do I go wrong?
  Also please explain how to rectify it? I'm new to this language and this is my first ever build I'm trying.</p>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <pre><code>function read_score()
  
  local f1 = assert(io.open(path, "r"))
  local contents = f1:read( "*a" )
  highScore = tonumber(contents)
  f1:close()  
  if highScore==nil  
  then highScore=0
  elseif score&gt;highScore
      then
      highScore=score
      local f2=assert(io.open(path, "w"))
      f2:write(highScore)
      f2:close()
  end
  
  
  end
  
  
  function disp_permScore()
  local f1 = assert(io.open(path, "r"))
  local contents = f1:read( "*a" )
  highScore = tonumber(contents)  
  
  text_display2= display.newText("GAME OVER!\n BEST: " ..highScore, 0, 0, "native.systemFontBold", 80)
  text_display2.x = centerX
  text_display2.y = centerY
  text_display2.alpha=1 
  f1:close()
  end
  
  
  function gameOver()
  
  mainScreen()
  disp_permScore()
  </code></pre>
  
  <p>Please heave a look above at the edited code now. Now whenI run this code by using an old file( which had been opened earlier, it runs good then saving the code permanently).. but when I try opening a new file, the code fails. (I think thats because I call the read_score() and disp_permScore() functions which initially opens file in 'read' mode - is raising the error) But then, how to rectify this error?
  P.S : when I change "r" mode to "r+" mode, the same error rises again.
  Please help</p>
  
  <p>EDIT 2:</p>
  
  <pre><code>function saveScore()
  local path = system.pathForFile("scoredata001.txt", system.DocumentsDirectory)
  local file = io.open(path, "w")
  if file then
      local score=get_score() --The get_score() returns the value of current score which is saved in 'score'.
      local newScore = compareScore()
      local contents = tostring( newScore )
      file:write( contents )
  
      io.close( file )
      return true
  else
      print("Error: could not write Score")
      return false
  end
  end
  
  
  function loadScore()
  local path = system.pathForFile("scoredata001.txt", system.DocumentsDirectory)
  
  local contents = ""
  local file = io.open( path, "r" )
  if file then
  
       local contents = file:read( "*a" )
       local score = tonumber(contents);
       io.close( file )
       return score
  end
  print("Could not read scores from scoredata.txt")
  return nil
  end
  
  function return_highScore()
  local highscore=loadScore()
  if highscore==nil
      then highscore=0
  end
  return highscore
  end
  
  function compareScore()
  local highscore=return_highScore()
  if highscore
      then
  local currscore=get_score()
  if highscore==0
      then
      return highscore
  elseif currscore&gt;highscore
      then 
      return currscore
  end
  end
  return true
  end
  
  
  function disp_permScore()
  local display_score=return_highScore()
      text_display2= display.newText("GAME OVER!\n BEST: " ..display_score, 0, 0, "Helvetica", 80)
      text_display2.x = centerX
      text_display2.y = centerY
      text_display2.alpha=1 
  
  function gameOver()
  
  mainScreen()
  saveScore()
  disp_permScore()
  end
  </code></pre>
  
  <p>Please have a look at this one?
  Since I wanted to display both high score and current score, I modified the previous score.. Current score is being displayed perfect. This is something I tried yesterday night. 
  But now, the high score is not being saved in file. (i.e. the best always displays 0)
  Also, the cmd says "unable to read scores form scoredata.txt)
  I'm unable to find where I went wrong. 
  Please help with this?</p>