1

I'm making a program that uses a lot of variables and changes them constantly.

How to save those variables into another file from inside the program?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Happymyself
  • 29
  • 1
  • 3

2 Answers2

1

You have to use io.open(filename, mode) to create a file handle, then use :write(linecontent) and :read("*line") to write and read in order. From there you can "load" and "save" variables by keeping track of the line orders for each variable you use:

local f = assert(io.open("quicksave.txt", "w"))
f:write(firstVariable, "\n")
f:write(secondVariable, "\n")
f:write(thirdVariable, "\n")
f:close()
local f = assert(io.open("quicksave.txt", "r"))
firstVariable = f:read("*line")
secondVariable = f:read("*line")
thirdVariable = f:read("*line")
f:close()
Unihedron
  • 10,902
  • 13
  • 62
  • 72
0

Best way would be to put the variables inside a table and then use textutils.serialize like this:

To save it do:

local file = fs.open("filename", "w")
file.write(textutils.serialize(your_table))
file.close()

To load it do:

local file = fs.open("filename", "r")
your_table = textutils.unserialize(file.readAll())
file.close()
Mattahy
  • 23
  • 4