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

[Lua][Error] saving and loading kills my Variables

Started by sees1dk, 25 October 2012 - 07:00 PM
sees1dk #1
Posted 25 October 2012 - 09:00 PM
Hello everyone, I am new to coding in lua, I'm sorry if my code is .

I made this code to tell the last time was a shift on redstone signal, and save the time.
but wen i added the saving and loading it keeps on giving me this message:
"on2:52: attempt to concatenate nil and string"

i have be troubleshooting now for 2 hours and can not find what wrong.

Hops someone can help me. ^_^/>/>


local S = 0 M = 0 ST = 0 Y = 0

local load = io.open("data/On2_data", "r")
local save = io.open("data/On2_data", "w")

S = load:read()
M = load:read()
Y = load:read()
load:close() 

while true do   

local x = (redstone.getInput ("left"))
  
  if x==False then 
  
    if Y == 0 then 
      ocal S = 0 M = 0 Y = 1
    end
      term.clear()
      term.setCursorPos(1,1)
      
  term.clear()
  term.setCursorPos(1,1)
   print ("The alarm is OFF")
   print (" Is "..S.." seconds") 
   print ("and "..M.." minues") 
   print ("since last error")
      
 os.sleep(1)
      S = S + 1
      local x = (redstone.getInput ("left"))
      if S == 60 then
        S = S-60 
        M = M+1
      end
 ST = ST + 1
 if ST == 15 then
        ST = ST - 15
        save:write(S.."\n")
        save:write(M.."\n")
   save:write(Y)
   save:close()
      end
  else
    if Y == 1 then 
      local S = 0 M = 0 Y = 0
    end
      term.clear()
 term.setCursorPos(1,1)
      print("The alarm is ON")
 print (" Is "..S.."seconds")  
 print ("and "..M.."minues")
 print("since an error occured" )
      local x = (redstone.getInput ("left"))
      shell.run ("redpulse", "right", "1", "1")
      S = S+1
 if S == 60 then
        S = S-60 
   M = M+1
 end
 if ST == 15 then
        ST = ST - 15
        save:write(S.."\n")
        save:write(M.."\n")
   save:write(Y)
   save:close()
      end
  end 
end
OmegaVest #2
Posted 25 October 2012 - 09:09 PM
I see a whole bunch of small things that all could (and probably did) contribute to your problem.

First, none of s, m or y are properly defined. If they are on separate lines, then use readLine, not read, the latter of which brings in the whole file as a table onto that variable.
Second, if s and m are supposed to be numbers, use tonumber(whatever) to make them so.
Finally, in response to the error you gave us, don't concatenate. print allows you to use commas, so do this: print("Is " , s, " seconds"). Or something to that effect for the offending line, and similar to the lines that follow it.

There's probably more, but those are the pertinent problems.
ChunLing #3
Posted 26 October 2012 - 02:42 AM
You try to use "ocal S = 0 M = 0 Y = 1" The local is misspelled, but you don't want those to be local anyway, because they will then disappear at the end of the scope they were declared, which is the "if Y == 0 then … end", so they end being out of scope immediately after being declared.

Declare and initialize all the variables you'll want to use generally at the outset, so that they're in scope for the entire program.
sees1dk #4
Posted 26 October 2012 - 04:10 PM
Thank you for the input.
But it did not help to fix the problem.

i fix it myself and it working

what was wrong was the "load:read()" it should have been "load:read(l)".
I cleared the code a bit, too.