Jump to content




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


  • You cannot reply to this topic
8 replies to this topic

#1 Spongy141

  • Members
  • 526 posts
  • Location'Merica

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


#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

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?!

#3 ChunLing

  • Members
  • 2,027 posts

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.

#4 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 12 March 2013 - 11:01 AM

View PostLBPHacker, on 12 March 2013 - 10:59 AM, said:

{
	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.

#5 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 12 March 2013 - 11:04 AM

View PostChunLing, on 12 March 2013 - 10:59 AM, said:

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


#6 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

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.

#7 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 12 March 2013 - 11:34 AM

View PostLBPHacker, on 12 March 2013 - 11:06 AM, said:

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

#8 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 12 March 2013 - 12:12 PM

Meh, I already know everything you went over about tables + some. ^_^ Nice tutorial though.

#9 ChunLing

  • Members
  • 2,027 posts

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users