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

Having trouble with a startup program...

Started by SovietRussianSpy, 17 June 2015 - 09:46 PM
SovietRussianSpy #1
Posted 17 June 2015 - 11:46 PM
I'm not every good with lua nor this mod but I have been learning a bit, so if you decide to help me please be patient with me. I made a program that opens a room with a password. The only problem is, is the chunk isn't loaded. The computer resets and anyone is able to anything they like, which isn't good. I knew how to fix startup where it runs the program, but that ran into it's own problems. Like not being able to enter the code: 1234 and being able to go back and edit the program or add anything I'd like. I just wanted to see if anyone knew a way to fix this problem. The links to the password program and startup program is lined in the word program.
Quintuple Agent #2
Posted 18 June 2015 - 12:09 AM
The problem with you trying to exit out using your admin password is that once your door script has stopped, your run command in your startup file is in a loop and so it will run it again, you can just remove the loop and have the run command. Since the main part of your door program is in a while loop it should keep running until you call error() (you could also just use return )
Also another tip, in your code for correct password (123) you should have it reset x to 1

Edit: I just remembered that cc has that pesky terminate keypress <_</> however you can avoid the termination by putting

local oldEve = os.pullEvent
os.pullEvent = os.pullEventRaw
at the top of your code, this is make it so the termination event will not work
Then before you exit your script you can do

os.pullEvent = oldEve
to change it back to normal

And yet another edit: I noticed that the code already has the os.pullEventRaw, good, however it is a good idea to save the old pullEvent then return it to normal, so that still stands.

Ex:
Spoiler

while true do
shell.run("doorlock")
end
to

shell.run("doorlock")
and

if input == "123" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("The password entered was correct!")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Opening Gate...")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
else if input == "1234" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Admin password has been entered.")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("One moment...")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  error()
to

if input == "123" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("The password entered was correct!")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Opening Gate...")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
  x = 1 --Added so when the correct password is entered the incorrect counter resets
else if input == "1234" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Admin password has been entered.")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("One moment...")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  os.pullEvent = oldEve --Change the pullEvent back to normal
  return --You can use return instead of error()  (Just better practice since there is no real error
Edited on 17 June 2015 - 10:26 PM
SovietRussianSpy #3
Posted 18 June 2015 - 12:57 AM
Thanks so much for your help.
The problem with you trying to exit out using your admin password is that once your door script has stopped, your run command in your startup file is in a loop and so it will run it again, you can just remove the loop and have the run command. Since the main part of your door program is in a while loop it should keep running until you call error() (you could also just use return )
Also another tip, in your code for correct password (123) you should have it reset x to 1

Edit: I just remembered that cc has that pesky terminate keypress <_</> however you can avoid the termination by putting

local oldEve = os.pullEvent
os.pullEvent = os.pullEventRaw
at the top of your code, this is make it so the termination event will not work
Then before you exit your script you can do

os.pullEvent = oldEve
to change it back to normal

And yet another edit: I noticed that the code already has the os.pullEventRaw, good, however it is a good idea to save the old pullEvent then return it to normal, so that still stands.

Ex:
Spoiler

while true do
shell.run("doorlock")
end
to

shell.run("doorlock")
and

if input == "123" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("The password entered was correct!")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Opening Gate...")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
else if input == "1234" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Admin password has been entered.")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("One moment...")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  error()
to

if input == "123" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("The password entered was correct!")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Opening Gate...")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
  x = 1 --Added so when the correct password is entered the incorrect counter resets
else if input == "1234" then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowWrite("Admin password has been entered.")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("One moment...")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  os.pullEvent = oldEve --Change the pullEvent back to normal
  return --You can use return instead of error()  (Just better practice since there is no real error
Thanks so much for your help.