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

5+2time:105: attempt to index ? (a nil value)

Started by RaiuPlays, 14 February 2015 - 05:03 AM
RaiuPlays #1
Posted 14 February 2015 - 06:03 AM
I'm trying to write a program that calculates time and hourly rate then saves information to log files. http://pastebin.com/0dPB7jKH

The program runs perfectly until it gets to around line 95. I don't have a very good understanding of the fs API so I expected to make mistakes, but I don't know how to fix it or where the problem is. It's also very possible that I made more mistakes in the code toward the bottom of the program. Help would be greatly appreciated.
Lyqyd #2
Posted 14 February 2015 - 06:20 AM
The fs.open call returns a file handle, which you have to put into a variable. It looks like your code expects that variable to be h, so put the return value from your fs.open calls into that variable. For example, swap out line 104 for something like this:


local h = fs.open("rate", "w")
RaiuPlays #3
Posted 14 February 2015 - 06:37 AM
I looked at what you said, looked back at my program, and everything suddenly made sense. Thank you sir. I have one more question though. After I fixed it, I checked the log and the total time was written with decimal points, like this: "11.0:16.0:21.0". What should I do to make those whole numbers without decimal points?
KingofGamesYami #4
Posted 14 February 2015 - 07:24 AM
I suggest you look at this function. It'll let you save a table which can then be loaded using the unserialize counterpart function.

If you insist on using the current system, string.gmatch( str, pattern ) might be of interest. pattern tutorial.
Bomb Bloke #5
Posted 14 February 2015 - 11:03 AM
You should be able to change line 131 like so:

totaltime = tostring(hr)..":"..tostring(min)..":"..tostring(sec)

That would allow you to turn this sort of thing:

write("Total: ")
for q=1,5 do
  write(totaltime[q])
end
print("")

… into this:

print("Total: "..totaltime)
RaiuPlays #6
Posted 14 February 2015 - 05:08 PM
I actually just ended up changing line 174-177 to one line like this:

l.writeLine("Total: "..hr..":"..min..":"..sec)
That seems to have fixed it. It was only the log that was showing decimals, but not what appeared on the console when I run the program. Everything seems to be working properly now. Thank you all for the help!