Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5930447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:33:24+00:00 2026-05-22T14:33:24+00:00

Sorry if there’s already a topic like this, but I couldn’t find any that

  • 0

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:

hp = 25

file = io.open("player.txt","w")
if file==nil then
    io.output("player.txt")
    io.close()
end
file:write(hp)
file:close()

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:

25b[NUL]ÈñZ[NUL]
file = io.open("player.txt","w")

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.

Edit: Hmmh, seems like the whole code is messed, up it always rewrites the whole file ;o

Edit2: 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:

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

And I know it doest look anything like the code I first posted, but that’s what I basically meant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:33:25+00:00Added an answer on May 22, 2026 at 2:33 pm

    Sounds like you’re confused about the flags on io.open. Check the manual to be sure what you really want is the w flag since that overwrites everything.

    Trying to do a file:write when you’re in the if shouldn’t work, and I’m not sure why you’d expect it to, since file is nil. You’re saying that if the file couldn’t be opened, then try to write this to the file, which doesn’t make sense to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for this silly question, but is there any way to restrict using directives
Sorry if there is already made such question earlier, but I have no time
Sorry for this question. There is a lot of information but unfortunately I haven't
Sorry if this is a dupe. There are many similar questions but it seems
Sorry if this is a stupid question, but is there an easy way to
Sorry for this simple question, but I can't solve it... There is an example:
I'm sorry if there is a stupidly simple answer to this question, but I've
I'm sorry but there can't be an example so we'll have to take a
i'm sorry if there is a question like this i just wanna know, i'm
I didn't find something good that helps me so I'm asking a question, sorry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.