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

What's wrong with this code i made?

Started by tipton775, 23 December 2013 - 09:38 AM
tipton775 #1
Posted 23 December 2013 - 10:38 AM
redstone.setOutput("left", true)
correctpassword = "codered"
pin = "0759"
write("What is the current situation?  ")
logtime = (os.time)

local input = read("*")

if input == (correctpassword) then
write("Password correct!")
goto
redstone.setOutput("right", true) then
sleep(5)
redstone.setOutput("right", false)

elseif input == "editit" then
write("Launching editing mode...")
write("")

elseif input == (correctpassword) then
write "Are you sure? Enter pin "
if input == (pin) then
write("Access granted.")
redstone.setOutput("bottom", true)

else
write("Incorrect! Your attempt has been logged...")		
local file = fs.open("log", "w")
   file.write("Last attempt")
   file.write(logtime)
end
file.close()
print("Logged.")
sleep(5)
os.shutdown()
end

I just dont know what is wrong with it. Possible fix please.
Edited on 23 December 2013 - 02:35 PM
Lyqyd #2
Posted 23 December 2013 - 12:32 PM
What error message are you getting?
MKlegoman357 #3
Posted 23 December 2013 - 02:33 PM
If you get any error message ALLWAYS post it. Also, put your code in [code]Your code here[/code] tags. Read documentation about functions you use in your code, in this case os.time(). Indent your code.

Your error message is probably something like this:
<your file name>:11: '=' expected

Code in code tags look like this:

This text is in code tags

You can find documentation about all CC APIs and their functions here.

A tutorial for indentation can be found in Read This Post Before Asking Questions topic.

And now your code:


redstone.setOutput("left", true)
correctpassword = "codered"
pin = "0759"
write("What is the current situation? ")
logtime = (os.time) --// os.time is a function so you should call it. Also, you don't have to use parenthesis around os.time
--// Should be
logtime = os.time()

local input = read("*")

if input == (correctpassword) then --// Could be this instead: (But it's your own choice if you want them or not)
if input == correctpassword then
write("Password correct!")
goto --// There is no 'goto' in Lua 5.1
redstone.setOutput("right", true) then --// I don't think that this 'then' should be here
sleep(5)
redstone.setOutput("right", false)

elseif input == "editit" then
write("Launching editing mode...")
write("")

elseif input == (correctpassword) then --// You check if your password was correct above, so this code will never run
write "Are you sure? Enter pin "
if input == (pin) then
write("Access granted.")
redstone.setOutput("bottom", true)

else
write("Incorrect! Your attempt has been logged...") 
local file = fs.open("log", "w")
file.write("Last attempt")
file.write(logtime)
end --// I also think that this 'end' shouldn't be here
file.close()
print("Logged.")
sleep(5)
os.shutdown()
end

You're lucky because I'm in a good mood, but next time Read This Post Before Asking Questions.

Note to Lygyd: Fix BBCode in this post. Here is what I see
wieselkatze #4
Posted 23 December 2013 - 02:34 PM
First of all I don't know, why there is a "goto" in line 11.
Also there is a "then" too much after the redstone.setOutput() in line 12.

Your logging function also won't operate as intended, because logtime calls os.time, but not os.time()
It won't even be launched, because the elseif input == (correctpassword) in line 20 makes no sense (there is already one in line 9).
Possible fix would be: http://pastebin.com/NzTJKnMz
tipton775 #5
Posted 23 December 2013 - 03:35 PM
Thanks for all that! I would have never thought, again thanks for all the suggestions