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

[error][question]

Started by ewart4fun, 10 September 2012 - 02:08 PM
ewart4fun #1
Posted 10 September 2012 - 04:08 PM
i want to make a program that counts the time people has been near the computer.
but i want to save the information at a server restart so i used fs.open, but i get the error message:
Spoiler

startup:8: attempt to perform arithmetic __add on table and number
this is my code
Spoiler


s1 = fs.open("sec", "w")
m1 = fs.open("min", "w")

term.clear()
term.setCursorPos(1, 1)
print("people has been around spawn for ", m, ":", s, " seconds")
sleep(1)
s1 = s1 + 1
fs.delete("sec")
s1.writeLine(s)
if s1 == 60 then
m1 = m1 + 1
fs.delete("min")
m1.writeLine(m)
fs.delete("sec")
s1 = 0
shell.run("startup")
end
ardera #2
Posted 10 September 2012 - 04:33 PM
First Issue: s1+s1 is the same as fs.open("sec", "w") + fs.open("sec", "w")

I think you wanted to code somethings like this:

local s=fs.open("sec", "r")
s1=assert(tonumber(s.readLine()), "Sec file corrupt")
s.close()
local m=fs.open("min", "r")
m1=assert(tonumber(m.readLine()), "Min file corrupt")
m.close()
term.clear()
term.setCursorPos(1, 1)
print("People has been around spawn for "..m1..":"..s1.." seconds")
sleep(1)
s1=s1+1
fs.delete("sec")
if s1 >= 60 then
    s1=0
    m1=m1+1
end
s=fs.open("sec", "w")
s.writeLine(tostring(s1))   -- s1 is currently a char, so it has to be converted to a string, by using tostring(number)
s.close()
m=fs.open("min", "w")
m.writeLine(tostring(m1)) --same with m1
m.close()
shell.run("startup")

OmegaVest #3
Posted 10 September 2012 - 04:33 PM

function saveTime(secons, minuns)
   s1 = fs.open("sec", "w")
   s1.write(secons)
   s1.close()

   m1 = fs.open("min", "w")
   m1.write(minuns)
   m1.close()
end
function loadTime(whichOne)
   if whichOne == "s" then
      s1 = fs.open("sec", "r")
      local outter = tonumber(s1.readLine())
      s1.close()

   elseif whichOne == "m" then
      m1 = fs.open("min", "r")
      local outter = tonumber(m1.readLine())
      m1.close()
   end
   return outter
end

while true do
   local secs = loadTime("s")
   local mins = loadTime("m")

   secs = secs + 1
   if secs == 60 then
      mins = mins +1
      secs = 0
   end

   term.clear()
   term.setCursorPos(1,1)

   print("People have been around Spawn for", mins, " minutes and ", secs, " seconds")

   sleep(1.0)

   saveTime(secs, mins)

If any of that confuses you, or you want to know why I made certain changes, pm me I don't have time right this second, but I want to help you understand if you do not.