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

[Lua][Error] Erggg, Can't get something to print out.

Started by Skylar Trollvaar, 02 January 2013 - 01:37 PM
Skylar Trollvaar #1
Posted 02 January 2013 - 02:37 PM
I play on Tekkit with my friends, and I made a password protected door successfully, but I want it to print a message out when they type "hint" in the password input (instead of the actual password). The code has no errors when I run it, yet it does nothing when I type hint in.

My code is as follows, (please note: I'm a beginner at this, so my code might be a bit messy.)


while true do
term.clear()
term.setCursorPos(1, 1)
print ("A password is required:")
print ("Forgot the pasword?  Type hint.")
local input = read("*")
if input == "password" then
  redstone.setOutput("back", true)
  sleep(5)
  redstone.setOutput("back", false)
else if input == "hint" then
print ("It has pass in it.")
end
end
end


I had to put two ends for some reason, otherwise it would say "end" expected at line 14 (where the second to last end is at the moment) to execute while true do or something like that.

Anyways, how can I make it print out the "It has pass in it." if they type hint?

Thank you:)!
Kingdaro #2
Posted 02 January 2013 - 02:47 PM
If you change the "else if" to "elseif" on line 11, you won't need the extra end.

Also, it is printing the "it has pass in it", but there's no sleep afterwards. After it prints, it immediately clears the screen and restarts the loop. Just put a "sleep(2)" after the print and you should be good.
Skylar Trollvaar #3
Posted 02 January 2013 - 02:48 PM
If you change the "else if" to "elseif" on line 11, you won't need the extra end.

Also, it is printing the "it has pass in it", but there's no sleep afterwards. After it prints, it immediately clears the screen and restarts the loop. Just put a "sleep(2)" after the print and you should be good.

Okay, I'll try that, thank you :)/>.

Edit: Thank you, it works :)/>. Is there anywhere to make the text appear at the top though, it appears almost in the middle of the screen :\.
Kingdaro #4
Posted 02 January 2013 - 02:53 PM
Just clear the screen before printing, simple. :D/>/>

elseif input == "hint" then
term.clear()
term.setCursorPos(1,1)
print ("It has pass in it.")
sleep(2)
end
Skylar Trollvaar #5
Posted 02 January 2013 - 03:05 PM
Oh, I see, Thanks! :D/>. And..sorry to hit you with another question here, but my friend has a screen, and I hooked a computer to it..and I want to make text appear on the screen…How would I go about doing this, I cannot find any guides..my guess on it came back with some wierd error that says "Startup:2: Attempt to index ? (a nil value)"..Help :S
Kingdaro #6
Posted 02 January 2013 - 03:35 PM
In order to write to monitors, you have to first get a monitor "object" using peripheral.wrap.


local mon = peripheral.wrap('right')

In this example, the variable "mon" is an object that represents our monitor on the right side of the computer. We can perform various operations on this object:


mon.clear()
mon.setCursorPos(2,2)
mon.write("Hello World!")

This clears the monitor's screen, goes to x 2 y 2, and writes "Hello World!" on the monitor.

More information on monitor functions: http://computercraft.info/wiki/Monitor#Peripheral_Functions