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

Door Lock Code Issues

Started by Spyke1354, 30 March 2017 - 01:38 AM
Spyke1354 #1
Posted 30 March 2017 - 03:38 AM
I have been trying to figure out what is wrong with this code for the past hour and a half, I am new to computer craft and coding in general, I need help.
https://postimg.org/image/pfcm65l2x/
https://postimg.org/image/487a443ct/
https://postimg.org/image/5rp6m3jmr/
TheRockettek #2
Posted 30 March 2017 - 07:15 AM
remove the end after input = read("*")

and after term.setCursorPos(1,1)

to use if statements you only use end at the, end

so

if something == something else then
– stuff
end

but if there is an elseif or else that isnt the end so you dont put one there

if something == somethingelse then
– stuff
elseif something == anotherthing then
– another thing
else
print("None is correct.")
end


os.pullEvent = os.pullEventRaw
local pullEventRaw = os.pullEventRaw
local doorLocation = "right"
local password = "test"
local sleepTime = 4
while true do
textutils.slowPrint("Enter Your Password: ")
local input = read("*")
if input == password then
  textutils.slowPrint("Access Granted")
  redstone.setOutput(doorLocation,true)
  sleep(sleepTime)
  term.setOutput(doorLocation,false)
  term.clear()
  term.setCursorPos(1,1)
elseif input == "backdoor" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("Welcome To The Termina")
  os.pullEvent = pullEventRaw
  sleep(1)
else
  textutils.slowPrint("Access Denied")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
end
end
ReBraLaCC #3
Posted 30 March 2017 - 10:17 AM
the error is caused because you ended the if statement 1 line above it and i dont know if it matters but you have "read ("*")" and i think it should be "read("*")"
FuzzyLitchi #4
Posted 30 March 2017 - 12:01 PM
using 'read ("*")' is valid, but weird if the rest of the code is without a space between the function name and the parentheses
FuzzyLitchi #5
Posted 30 March 2017 - 12:09 PM
You have quite a few extra end's, you only need an end when you want to end an if statement or loop entirely


while true do
  input = read("*")
  if input == password then
	--do correct password stuff
  elseif input == "backdoor" then
	--do backdoor stuff
  else
	--do wrong password stuff
  end
end
Notice how every time the indentation go a level down, there is an end. There is no end before the else and elseif statment because they are part of the if statement, they build onto it.

Also note how the elseif comes before the else, this is because if the else was before, the computer would check to see if the input is the same as the password and if not then it would check the the next statement, if this statment is an else it would do that code and be done, but if it is an elseif it would check that code and then continue depending on the statment.

Generaly if statments go as follows,
first an if, any amount of elseif's (0 is an amount), optionally an else, an end

A more detailed version of the previous would be as follows
first an if, a condition and a then, any amount of elseif's, a condition and a then (0 is an amount), optionally an else, an end

I don't know if that made sense just ask if it didn't. :)/>
Edited on 30 March 2017 - 10:14 AM