5 posts
Posted 29 March 2013 - 11:01 PM
Hello guys I've been trying to make a password protected door on a server that I play on.
I want it to loop 3 times (3 guesses) before it ends the loop and outputs a redstone signal.
I've been working on it for a bit and just can't seem to figure it out (I'm a noob so don't go too hard)
os.pullEvent = os.pullEventRaw
local password = "password123"
local guess = 0
while do
term.clear()
term.setCursorPos(1,1)
write("Password: ")
local input = read("*")
if input == password then
term.clear()
term.setCursorPos(1,1)
print("Correct!")
rs.setOutput("right",true)
sleep(5)
rs.setOutput("right",false)
else
print("Try again...")
guess = guess + 1
if guess == 3 then
print("Good bye!")
rs.setOutput("back",true)
sleep(5)
rs.setOutput("back",false)
end
end
Again I'm not a pro coder or anything, but if you could point out my errors and tell me how I might fix them it would be much appreciated!
Thanks again!
231 posts
Posted 29 March 2013 - 11:09 PM
while loops use a true/false condition to determine whether they should keep running or not, in the form
while <condition> do
--Loop body
end
If you want the code to loop forever, or until exited with the break keyword, you can just use true for the condition.
620 posts
Location
Holland
Posted 29 March 2013 - 11:09 PM
What you do works but you need to do
while true do
Or you can do this
while not guess == 3
537 posts
Location
Copenhagen, Denmark
Posted 29 March 2013 - 11:11 PM
Or this:
for i = 1, 3 do
-- Do stuff
end
620 posts
Location
Holland
Posted 29 March 2013 - 11:17 PM
Yeh I know but just to keep it simple for him/her
5 posts
Posted 30 March 2013 - 01:53 AM
Thanks for the help guys I'll try it out.
5 posts
Posted 30 March 2013 - 02:04 AM
What you do works but you need to do
while true do
Or you can do this
while not guess == 3
So I tried what you mentioned here, I'm apparently not doing it right as I still am getting errors, does this look correct to you?
os.pullEvent = os.pullEventRaw
local password = "password123"
local guess = 0
while not guess == 3 do
term.clear()
term.setCursorPos(1,1)
write("Password: ")
local input = read("*")
if input == password then
term.clear()
term.setCursorPos(1,1)
print("Correct!")
rs.setOutput("right",true)
sleep(5)
rs.setOutput("right",false)
else
print("Try again...")
guess = guess + 1
while guess == 3 do
print("Good bye!")
rs.setOutput("back",true)
sleep(5)
rs.setOutput("back",false)
end
end
43 posts
Posted 30 March 2013 - 01:02 PM
What you do works but you need to do
while true do
Or you can do this
while not guess == 3
So I tried what you mentioned here, I'm apparently not doing it right as I still am getting errors, does this look correct to you?
os.pullEvent = os.pullEventRaw
local password = "password123"
local guess = 0
while not guess == 3 do
term.clear()
term.setCursorPos(1,1)
write("Password: ")
local input = read("*")
if input == password then
term.clear()
term.setCursorPos(1,1)
print("Correct!")
rs.setOutput("right",true)
sleep(5)
rs.setOutput("right",false)
else
print("Try again...")
guess = guess + 1
while guess == 3 do
print("Good bye!")
rs.setOutput("back",true)
sleep(5)
rs.setOutput("back",false)
end
end
try this:
while guess < 3 do
--code
guess = guess + 1
that is what I know, and what error are you getting?
620 posts
Location
Holland
Posted 30 March 2013 - 07:24 PM
You miss an end of the if statement
1852 posts
Location
Sweden
Posted 31 March 2013 - 12:21 PM
Uhmm.. Here's a code that should work..
Spoiler
Added a function named cleanScreen() that
is basically the term.clear() term.setCursorPos(1,1)
But you just have to type cleanScreen()
os.pullEvent = os.pullEventRaw
local password = "password123"
guess = 0
function cleanScreen()
term.clear()
term.setCursorPos(1,1)
end
function main()
for i = 1,3 do
cleanScreen()
write("Password: ")
local input = read("*")
if input == password then
cleanScreen()
print("Correct!")
rs.setOutput("right",true)
sleep(5)
rs.setOutput("right",false)
elseif input ~= password then
print("Try again...")
guess = guess + 1
sleep(1)
end
end
if guess == 3 then
cleanScreen()
print("Good bye!")
rs.setOutput("back",true)
sleep(5)
rs.setOutput("back",false)
end
end
main()
Uhmm.. Here's a code that should work..
Spoiler
Added a function named cleanScreen() that
is basically the term.clear() term.setCursorPos(1,1)
But you just have to type cleanScreen()
os.pullEvent = os.pullEventRaw
local password = "password123"
guess = 0
function cleanScreen()
term.clear()
term.setCursorPos(1,1)
end
function main()
for i = 1,3 do
cleanScreen()
write("Password: ")
local input = read("*")
if input == password then
cleanScreen()
print("Correct!")
rs.setOutput("right",true)
sleep(5)
rs.setOutput("right",false)
<p> %2