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

Program restarts instead of doing what it should do

Started by kahrkunne, 30 December 2012 - 05:25 AM
kahrkunne #1
Posted 30 December 2012 - 06:25 AM
So this is my code. Of course there is an almost identical piece of code above this, and that likely has the same problem.
Thing is, it asks wether or not you want to turn the defences on, and it lets you enter the password should you choose yes, but after you have entered the password the program just restarts - the code begins again from the first line.
I have no idea what I am doing wrong here, I have no experience in LUA (a bit of Java experience is all I've got) and the whole "ending an if-statement with an end-statement" thing that LUA has going on is pretty foreign to me. There are a total of 5 if-statements in the whole code, and that's pretty confusing to me (as I'm used to starting and stopping if-statements with brackets)
EDIT: I am uploading a video as we speak, showing all of the code and exactly what is going on when I execute it.
http://www.youtube.com/watch?v=KHiN4aHUD3Q
This is a video of the code and what happens when I excecute it.
remiX #2
Posted 30 December 2012 - 06:30 AM
I can't see anything wrong with the code in the attached picture (ps, nice name you saved it as, lol)

Also, when you ask for if you want to turn it on or off, put the inputonpass within the if function of if input3 == "Y" then, or else it will ask for a password even if you type N, or anything else
Cozzimoto #3
Posted 30 December 2012 - 06:31 AM
its hard to read cause the lack of indentation but it seems like that in the block of checks [elseifs] and such nothing is yet satisfied so it goes to the very last else statement as that always returns if nothing else in the block of code isnt satisfied. so you prolly have to include another check, or check where its returning the reboot when you dont want it to.

maybe set a variable as restart = false
and if the password faisl then return restart = true.
kahrkunne #4
Posted 30 December 2012 - 06:43 AM
Sec let me record what exactly is going on and upload that. Maybe that'll help
EDIT: video is being uploaded. Also, entering a wrong password DOES give the correct response.
EDIT2: Video is uploaded.
kahrkunne #5
Posted 30 December 2012 - 07:28 AM
BUMP
remiX #6
Posted 30 December 2012 - 07:54 AM
I'll watch the video after this video of Simon and Louis on Tekkit :P/> Won't be long.

Ok so after watching the video, I see you just want it to ask if you want to enable when it's disabled and visa versa.

Try this code, I haven't tested it but I'll test it now and make sure it works. But it should.


while true do
    term.clear()
    term.setCursorPos(1,1) -- This will reset the text back to the top left, the the next text to be printed doesn't keep going down
    b_redStone = rs.getInput("bottom")
    print("Welcome to the Aparture Defense Initiative!")
    print("The defenses are currently " .. (b_redStone and "on!" or "off!")) -- Easier to do it like this instead of using ifs and elses
    print("Would you like to turn them " .. (b_redStone and "off" or "on") .. "? Y/N ") -- Easier to do it like this instead of using ifs and elses
    input = string.lower(read("*"))
    if input == "y" then
        print("Please enter password!")
        password = read("*")
        if password == "password" then
            rs.setOutput("bottom", not b_redStone) -- Just changes the state of the redstone signal to the oppisite of what it is
            print("\nThe defenses have been " .. (not b_redStone and "enabled!" or "disabled!")) -- \n skips a line
        else
            print("\nIncorrect password!") -- \n skips a line
        end
    end
    sleep(1.5) -- So user can see the text before clearing
end

Edit: Yeah it works fine
kahrkunne #7
Posted 30 December 2012 - 08:42 AM
Your code works amazing, thanks a lot!