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

Need help with Multiple Actions then Password Script

Started by Mega_Miner_Tom, 21 June 2013 - 09:09 AM
Mega_Miner_Tom #1
Posted 21 June 2013 - 11:09 AM
Hi Im new to the Forum (Obviously) but I was aiming to set up a "script" that would allow someone to pick an option (on/off) but the password would be the same for each option:


print("Select Action:")
write("Action: ")
password = read()

if Action == "On" then
print("Password Needed")
sleep(3)
Write("Password: ")
password = read()

if Password == 123" then
print("Water Pump Activating...")

sleep(3)
redstone.setOutput("top", true)
os.shutdown()
end

if Action == "Off" then
print("Password Needed")
sleep(3)
write("Password: ")
password = read("*")

if Password == "123" then
print("Water Pump De-Activating...")
sleep(3)
redstone.setOutput("top",false)
os.shutdown()
end

else
print("Error Incorrect Password...Shutting Down")
sleep(3)
os.shutdown()
end

I get the error :37: "end" expected (to close if at line 5)

Ive tried expermenting but just get a series off errors, Thanks Tom :D/>

~CraftOS 1.5~~FTB, Ultimate~
Edited by
Lyqyd #2
Posted 21 June 2013 - 12:38 PM
Split into new topic.

Try getting the action to do, then checking to see if it's "on" or "off", and if so, asking for the password, and then use an if block to perform the correct action:


--get action
if action == "on" or action == "off" then
  --get password
  if password == correctPass then
    if action == "on" then
      --perform on action
    elseif action == "off" then
      --perform other action
    end
  end
end
TheOddByte #3
Posted 21 June 2013 - 01:09 PM
You also did this wrong

if Password == 123" then

It should be

if Password == "123" then

So you forgot the "

And I fixed the code for you and changed it a little


cPass = "123" --Here's the password

function clear()
  term.clear()
	term.setCursorPos(1,1)
end

function drawAt(x,y,text)
  term.setCursorPos(x,y)
	write(text)
end




while true do --Do this instead of shutting down the computer
clear()
drawAt(1,1,"Select Action:")
drawAt(1,3,"Action: ")
action = read()

if action == "On" then
print("Password Needed")
sleep(1)
clear()
write("Password: ")
password = read()

if password == cPass then
print("Water Pump Activating...")

sleep(1)
redstone.setOutput("top", true)

elseif cPass ~= pass then
drawAt(1,4,"Password was incorrect!")
sleep(1)

end

elseif action == "Off" then
print("Password Needed")
sleep(1)
clear()
write("Password: ")
password = read("*")

if password == cPass then
print("Water Pump De-Activating...")
sleep(1)
redstone.setOutput("top",false)

elseif cPass ~= pass then
drawAt(1,4,"Password was incorrect!")
sleep(1)
end

else
print("Unknown Action!")
sleep(1)
end
end
Mega_Miner_Tom #4
Posted 21 June 2013 - 03:52 PM
You also did this wrong

if Password == 123" then

It should be

if Password == "123" then

So you forgot the "

And I fixed the code for you and changed it a little


cPass = "123" --Here's the password

function clear()
  term.clear()
	term.setCursorPos(1,1)
end

function drawAt(x,y,text)
  term.setCursorPos(x,y)
	write(text)
end




while true do --Do this instead of shutting down the computer
clear()
drawAt(1,1,"Select Action:")
drawAt(1,3,"Action: ")
action = read()

if action == "On" then
print("Password Needed")
sleep(1)
clear()
write("Password: ")
password = read()

if password == cPass then
print("Water Pump Activating...")

sleep(1)
redstone.setOutput("top", true)

elseif cPass ~= pass then
drawAt(1,4,"Password was incorrect!")
sleep(1)

end

elseif action == "Off" then
print("Password Needed")
sleep(1)
clear()
write("Password: ")
password = read("*")

if password == cPass then
print("Water Pump De-Activating...")
sleep(1)
redstone.setOutput("top",false)

elseif cPass ~= pass then
drawAt(1,4,"Password was incorrect!")
sleep(1)
end

else
print("Unknown Action!")
sleep(1)
end
end

Dosent work I took out the – tags then it launches normally??

Split into new topic.

Try getting the action to do, then checking to see if it's "on" or "off", and if so, asking for the password, and then use an if block to perform the correct action:


--get action
if action == "on" or action == "off" then
  --get password
  if password == correctPass then
	if action == "on" then
	  --perform on action
	elseif action == "off" then
	  --perform other action
	end
  end
end

Howerver this worked fine thanks :D/>
TheOddByte #5
Posted 21 June 2013 - 07:13 PM
Huh.. Strange… I tested the code In ccEmu and did'nt get any errors.. What was the problem?
apemanzilla #6
Posted 21 June 2013 - 09:12 PM
You were missing a couple quotes, and, as the error said, you forgot an "end" statement.
There's a lot of information in errors. Your error, "end expected to close if at line 5," was telling you that you had an if statement on line 5, and that the computer couldn't find an "end" statement telling it where the if statement finished.