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

Saving Usernames From Player Detector

Started by bigbaddevil6, 07 November 2013 - 02:45 AM
bigbaddevil6 #1
Posted 07 November 2013 - 03:45 AM
So when I try to execute this I get a attempt to index (a nil value) during the actual writing to the file. My plan is to take the username passed from the player detector insert it into a table then save that table to a file for record keeping more or less. Its been a while since I done saving and loading in Lua so can't remember the basics, but I assume I had this right. Is it the way the player detector works?


users = {}

while true do

  event, username = os.pullEvent("player")
  table.insert(users, username)
  local output = textutils.serialize(users)
  local handle = fs.open("Users", "w")
  handle.write(output)
  handle.close()

end
Lyqyd #2
Posted 07 November 2013 - 12:32 PM
Try adding a couple debug prints to ensure you're getting a valid file handle back. You could try changing the file name to "/Users" to ensure that it is in the root of the drive, especially if you are running from rom.
bigbaddevil6 #3
Posted 07 November 2013 - 06:35 PM
So I tried doing the /user and tried changing the directory, but it will not save. I also tried entering a item to the list manually and still won't save. The table seems to store the names fine, I can print them out and all that, but keeps saying that there is nothing there to save. Am I missing a step? I haven't even had any luck with just saving strings without using tables. I'm gonna start adding the debugs to see if I can catch it but as far as I can tell everything is where it should be.

-Update- adding some debug turns out that the
local handle = fs.open("Users", "w")
is the part where it is failing. So exactly… what is wrong with it? The file is there 100% sure and tried "/Users" still no luck.
Edited on 07 November 2013 - 09:57 PM
Grim Reaper #4
Posted 07 November 2013 - 11:36 PM
fs.open is returning nil because the file that you're trying to open or create cannot be opened or created. Some simple fixes are to restart the in game computer, reload the world, or restart minecraft.

Some computer craft related fixes (more fun :D/>) are to make sure that the file "/Users" isn't a directory and hasn't been opened previously by another program without having the handle to it closed properly. For example, if you have some startup file that opens the "/Users" file but doesn't call close() on it, then you won't be able to open the file until that handle is closed. To make sure that it isn't a problem with the fs API, try running the same code with a different output path, like "/testFile" or something.
bigbaddevil6 #5
Posted 08 November 2013 - 03:21 PM
After messing with it and doing like you said, I was able to get it working. Only thing I can think happen was one of the times when the program stopped it opened the file and never got a chance to close it.