This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
notyepenyar's profile picture

need help saving a variable to a file

Started by notyepenyar, 01 February 2013 - 09:36 AM
notyepenyar #1
Posted 01 February 2013 - 10:36 AM
title: need help saving a variable to a file

I want to save a variable to a file so that it can be saved if the program restarts.
Please explain why this doesn't work.
h = fs.open("variablex", "w")
x = 5
h.write(x)
h.close()
Mailmanq! #2
Posted 01 February 2013 - 03:13 PM
It works just fine, but it writes 5.0 just do this to fix it


local h = fs.open("variablex", "w")
local x = 5
h.write(tostring(x))
h.close()
notyepenyar #3
Posted 02 February 2013 - 08:45 AM
Thanks, btw, what does the local mean in front of stuff, is it necessary in certain places?
Kingdaro #4
Posted 02 February 2013 - 08:47 AM
It's never really completely necessary, but is almost always the safest thing to do. If a variable isn't local, you might end up overwriting a global API, or a variable that a program/function depends on.