645 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
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
758 posts
Location
Budapest, 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?!
2005 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.
645 posts
Location
'Merica
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.
645 posts
Location
'Merica
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
758 posts
Location
Budapest, 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.
1522 posts
Location
The Netherlands
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/>
1511 posts
Location
Pennsylvania
Posted 12 March 2013 - 12:12 PM
Meh, I already know everything you went over about tables + some. ^_^/> Nice tutorial though.
2005 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.