63 posts
Posted 11 October 2013 - 07:47 PM
So I have this code here:
http://pastebin.com/ep7wkvMUAnd I don't know why it is giving me errors.
Please help?
500 posts
Posted 11 October 2013 - 07:51 PM
Just looking at it, I think it might be because you're calling h.readAll twice. I'm not entirely sure though.
331 posts
Posted 11 October 2013 - 09:35 PM
your code
Spoiler
-- Clear the screen.
term.clear()
term.setCursorPos(1, 1)
-- Open the password containing file.
fs.open("/word", "r")
local y = h.readAll()
-- Print a prompt asking for password...
print("Please enter the password: ")
local pass = h.readAll()
-- Display the users password as stars.
y = read("*")
if y == pass then
-- VALID PASSWORD
print("Password is VALID!")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
sleep(1)
os.shutdown()
else
-- PASSWORD INVALID
print("Password is INVALID!")
sleep(10)
os.shutdown()
end
theres a couple of things you do wrong
1. do this
local h = fs.open("/word","r")
so that it saves the data fs.open() returns
2. why do you have
y = h.readAll()
when it gets overriden here
y = read("*")
3. close your file handle at the end like so
h.close()
so an overall better code would be
Spoiler
-- Clear the screen.
term.clear()
term.setCursorPos(1, 1)
-- Open the password containing file.
local h = fs.open("/word", "r")
-- Print a prompt asking for password...
print("Please enter the password: ")
local pass = h.readAll()
h.close()
-- Display the users password as stars.
y = read("*")
if y == pass then
-- VALID PASSWORD
print("Password is VALID!")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
sleep(1)
os.shutdown()
else
-- PASSWORD INVALID
print("Password is INVALID!")
sleep(10)
os.shutdown()
end
1852 posts
Location
Sweden
Posted 12 October 2013 - 07:18 AM
A tip, If you are getting an error and want people to help you, you should always post what error you are getting, and which line the error is on.
It makes it so much easier to help you, But my guess is that you haven't event used h when opening the file
fs.open("/word", "r")
Should be
h = fs.open("/word", "r")
And close the file as mentioned above
h.close()
Since then you can call h.readAll() etc.
And I have no clue why you are doing this
local y = h.readAll() -- Why are you doing this?
-- Some code..
y = read("*") -- When you are overriding 'y' here?
63 posts
Posted 15 October 2013 - 06:17 PM
Wow, I feel slightly dumb, I just ignored parts of the wikipage on the file reader. Sorry!