Posted 11 October 2012 - 07:01 AM
Hello. I'm trying to make a program that controls a redstone output of a certain side. I've been trying to get it all to work, and it's just giving me a lot of trouble. The entire point of having a file control the redstone output is that it will open up the previous state of the output on startup. What I'm trying to have it do is create the directory(easy), then read the file, and if the first line = nil, then it sets a variable to false, and it writes false into the file later on. Then, it reads again and depending on what it is it will display "On" or "Off" (true or false respectively). When you toggle it, it changes the value to the opposite of what it is, and thus displays the opposite thing (which I know how to do). I'm getting one of those "attempt to index ? (a nil value)" things on the marked line:
I also notice that there is no "top" file in the RSStates directory when I actually look it up. So, it's messing up, and I can't figure out why. Though I feel the answer is right in front of me and I can't see it. :P/>/>
Something honestly tells me that this is the wrong way to do all this, but I can't figure out how to get it to do this, if you could help me either fix this or make another way, I would be grateful.
Thanks in advance!
function saveTopRSState()
fs.makeDir("RSStates")
local rstopfile = fs.open("RSStates/top", "r")
local toprsdata = {}
local lineA = rstopfile.readLine() -- This line.
if lineA == nil then
isTopRSOn = false
else
repeat
table.insert(toprsdata,lineA)
lineA = rstopfile.readLine()
until line == nil
isTopRSOn = toprsdata[1]
end
local file = fs.open("RSStates/top", "w")
file.writeLine(isTopRSOn)
file.close()
end
function writeTopRSStatus(y)
term.setCursorPos(32,y)
saveTopRSState()
local rstopfile = fs.open("RSStates/top", "r")
local TopRSData = {}
local line = rstopfile.readLine()
repeat
table.insert(TopRSData,line)
line = rstopfile.readLine()
until line == nil
currentTopRS = TopRSData[1]
if currentTopRS == "true" then
term.write("ON ")
else
term.write("OFF")
end
end
I also notice that there is no "top" file in the RSStates directory when I actually look it up. So, it's messing up, and I can't figure out why. Though I feel the answer is right in front of me and I can't see it. :P/>/>
Something honestly tells me that this is the wrong way to do all this, but I can't figure out how to get it to do this, if you could help me either fix this or make another way, I would be grateful.
Thanks in advance!