Spoiler
[Lua][Error]DynationOS:19: attempt to index ? (a nil value)
Started by Spongy141, Mar 12 2013 10:45 AM
8 replies to this topic
#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.
#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?!
#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.
#4
Posted 12 March 2013 - 11:01 AM
LBPHacker, 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?!
#5
Posted 12 March 2013 - 11:04 AM
ChunLing, 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.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
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
Posted 12 March 2013 - 11:34 AM
LBPHacker, 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
#8
Posted 12 March 2013 - 12:12 PM
Meh, I already know everything you went over about tables + some.
Nice tutorial though.
#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 endThis 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.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











