title: Lua file reading and writing error
body: <p>Sorry if there's already a topic like this, but I couldn't find any that have something to do with Lua... So I'm basically having some problems in writing and reading files, here's what I've done:</p>
  
  <pre><code>hp = 25
  
  file = io.open("player.txt","w")
  if file==nil then
      io.output("player.txt")
      io.close()
  end
  file:write(hp)
  file:close()
  </code></pre>
  
  <p>and it seems to work fine, it's just perfect... but then when I'm trying to add the file:write(hp) inside the if-sentence, it doesn't work. Also if I'll add file:read("*line") right after file:write(hp), this is what it says in player.txt:</p>
  
  <pre><code>25b[NUL]ÈñZ[NUL]
  file = io.open("player.txt","w")
  </code></pre>
  
  <p>So what am I doing wrong? Also [NUL] is black block with white "NUL" text in it in notepad++ but it can't be copied here.</p>
  
  <p><strong>Edit</strong>: Hmmh, seems like the whole code is messed, up it always rewrites the whole file ;o</p>
  
  <p><strong>Edit2</strong>: Had actually no idea what I was talking about, nowadays I can understand file controlling bit more, here's what it should've been or what I tried to do:</p>
  
  <pre><code>function existsFile(path)
      x = io.open(path)
      if x == nil then
          io.close()
          return false
      else
          x:close()
          return true
      end
  end
  
  if not existsFile("player.txt") then
      file = io.open("player.txt", "w")
      file:write(25)
      hp = 25
      file:close()
  else
      file = io.open("player.txt", "r")
      hp = file:read("*number")
      file:close()
  end
  </code></pre>
  
  <p>And I know it doest look anything like the code I first posted, but that's what I basically meant.</p>