This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dark's profile picture

[error] lock code not working

Started by dark, 25 March 2012 - 07:24 AM
dark #1
Posted 25 March 2012 - 09:24 AM
ok i got a simple door lock code that i upgraded to use pcall to prevent ctrl t from bypassing the lock, but now it will not unlock for the correct password. Also in some cases the ctrl t will terminate the programm. i'm a noob at lua and computercraft programming so do explain if you help.


local pass = "pass"
local side = "left"
while true do
term.clear()
term.setCursorPos(1,1)
term.write("Password: ")
local imput = pcall(read, "x")
if imput == pass then
print("unlocked")
redstone.setOutput(side,true)
pcall(sleep, 5)
redstone.setOutput(side,false)
else
if imput == ("TERMINATED") then
term.clear()
term.setCursorPos(1,1)
print("access denied")
pcall(sleep, 10)
end
print("Incorrect")
pcall(sleep, 5)
end
end
kamnxt #2
Posted 25 March 2012 - 10:20 AM
The function pcall(read, "x") returns two values. The first one tells you if there's an error (i think). You want the second one:


local pass = "pass"
local side = "left"
while true do
term.clear()
term.setCursorPos(1,1)
term.write("Password: ")
local _, imput = pcall(read, "x")
if imput == pass then
print("unlocked")
redstone.setOutput(side,true)
pcall(sleep, 5)
redstone.setOutput(side,false)
else
if imput == ("TERMINATED") then
term.clear()
term.setCursorPos(1,1)
print("access denied")
pcall(sleep, 10)
end
print("Incorrect")
pcall(sleep, 5)
end
end
NeoHummel #3
Posted 25 March 2012 - 05:22 PM
pcall's first return value is a boolean that describes whether or not it encountered an error during exexution.
True means it completed without an error, false means there was an error, for instance someone pressed ctrl+t
dark #4
Posted 25 March 2012 - 06:37 PM
oh thank you, i noticed people were using that in there code but thought it as something else. Is there a site that actual explains some of this the computercraft and lua wiki's are not very helpful.