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

[Lua][Error]DynationOS:19: attempt to index ? (a nil value)

Started by Spongy141, 12 March 2013 - 09:45 AM
Spongy141 #1
Posted 12 March 2013 - 10:45 AM
Alright, first thing I did edit out the rest of the code, so all you guys will have to see, is the actual function with the error, I thought it would be mean of me to put almost 200 lines of code, also I # ed the code for you guys to, and it will be on line 8… not 19.
Spoiler


1.  function login()
2.   term.setTextColor(colors.blue)
3.   print("Account name: ")
4.   term.setTextColor(colors.white)
5.   local user = 
6.   { 
7.     file = fs.open("file.username","r"),  
8.     user = file.readLine(),
9.   }  
10.   local name = read()
11.   if name == (user) then
12.     file.close()
13.     term.setTextColor(colors.blue)
14.  print("Password: ")
15.  local pass = 
16.    { 
17.       file = fs.open("file.password","r"),
18.       pass = file.readLine()  
19.    }
20.      term.setTextColor(colors.white)
21.      local passw = read()
22.      elseif passw == (pass) then
23.      file.close()
24.     User()
25.   else
26.     file.close()
27.     term.setTextColor(colors.red)
28.     print("Sorry account does not exist")
29.     sleep(1.5)
30.     print("Redirecting you to setup")
31.     setup()
32.   end
33. end
LBPHacker #2
Posted 12 March 2013 - 10:59 AM

{
	file = fs.open("file.username","r"),  
	user = file.readLine(),
}

What on earth?!…

You should use

file = fs.open("file.username","r")
local user = file.readLine()

Where did you even get that table idea?!
ChunLing #3
Posted 12 March 2013 - 10:59 AM
You can't access a table from within it's own definition like that. The table isn't defined yet, so there's nothing to access. What you could do instead might be like:
local user = {file = fs.open("file.username","r")} --see, now the table is defined already
user.user = user.file.readLine()
That should work…actually I'm a bit unsure. Why not just make a function that takes a filename as a parameter and returns the username from it? As a bonus it will close the file when it's done.
Spongy141 #4
Posted 12 March 2013 - 11:01 AM

{
	file = fs.open("file.username","r"),  
	user = file.readLine(),
}

What on earth?!…

You should use

file = fs.open("file.username","r")
user = file.readLine()

Where did you even get that table idea?!
When I needed to have the string in the other file a define variable. So I though that, that would work… but instead I got an error.
Spongy141 #5
Posted 12 March 2013 - 11:04 AM
You can't access a table from within it's own definition like that. The table isn't defined yet, so there's nothing to access. What you could do instead might be like:
local user = {file = fs.open("file.username","r")} --see, now the table is defined already
user.user = user.file.readLine()
That should work…actually I'm a bit unsure. Why not just make a function that takes a filename as a parameter and returns the username from it? As a bonus it will close the file when it's done.
ugh… lol the program I am making already has a ton of functions, but yeah I could see how that could work better.
EDIT: I made it into a function, and it gave me the same error…


function name()
  file = fs.open("file.username","r")
  local user = {file.readLine()}
  file.close()
end
LBPHacker #6
Posted 12 March 2013 - 11:06 AM
OK, I see you love storing variables in files. Check out this tutorial, it'll help a bit.
Engineer #7
Posted 12 March 2013 - 11:34 AM
OK, I see you love storing variables in files. Check out this tutorial, it'll help a bit.

A bit? That tutorial would make you a pro with storing tables!!

Sorry for the unnessecary post, I just like his tutorial very much :P/>
SuicidalSTDz #8
Posted 12 March 2013 - 12:12 PM
Meh, I already know everything you went over about tables + some. ^_^/> Nice tutorial though.
ChunLing #9
Posted 12 March 2013 - 06:19 PM
You need to return the value, and you also need to use tables appropriately (or not use them). Like so:
function name(filename) -- accepts a string parameter
  local file = fs.open(filename,"r")
  local user = file.readLine() -- don't know why you were putting this in a table
  file.close()
  return user -- returns a string
end
This shouldn't error if you give it a valid filename (feel free to check if the filename exists before calling this if that's a problem). If you're getting that error, then it's because you aren't opening the file for some reason…quite possibly because it doesn't exist.