Search code examples
luaminecraftcomputercraft

Computercraft variables


I am making a bank on Minecraft.

I am having trouble with saving a variable after addition or subtraction has been done to it.

For example, if x="balance", x=15, say I want to withdraw from my balance:

x = 15 - y(withdrawn money)

The variable is not saved when the program is run again.


Solution

  • If you want data persistence between program runs, you need to store the data in files. For example, you could save the variable x to a file like this:

    h = fs.open("filename","w")
    h.writeLine(x)
    h.close()
    

    And you could load it like this:

    h = fs.open("filename","r")
    x = tonumber(h.readLine())
    h.close()
    

    Here is the documentation. http://computercraft.info/wiki/Fs.open