Jump to content




Door Lock Code Issues


  • You cannot reply to this topic
4 replies to this topic

#1 Spyke1354

  • New Members
  • 1 posts

Posted 30 March 2017 - 02: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/

#2 TheRockettek

  • Members
  • 547 posts
  • LocationRem is best girl

Posted 30 March 2017 - 06: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


#3 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 30 March 2017 - 09: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("*")"

#4 FuzzyLitchi

  • Members
  • 34 posts

Posted 30 March 2017 - 11:01 AM

using 'read ("*")' is valid, but weird if the rest of the code is without a space between the function name and the parentheses

#5 FuzzyLitchi

  • Members
  • 34 posts

Posted 30 March 2017 - 11:09 AM

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 by FuzzyLitchi, 30 March 2017 - 11:14 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users