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

term.exit() : Attempt to call nil

Started by GravityScore, 24 June 2012 - 06:46 AM
GravityScore #1
Posted 24 June 2012 - 08:46 AM
Hello.

This is not a question wanting you to write any code for me, but I would like to know why when I call
term.exit()
in my code, it stops the program executing (like it should), but I get the error:

program:1: attempt to call nil

Does anyone know why this is and how to stop it happening? I tried searching through the Lua term API, but I could not find the function exit(), so I could not find the source of the problem.

When ever I try
shell.exit()
it just does nothing. Anyone know why this is as well?

Thanks so much for any help! I've been pondering this for a while.
Mads #2
Posted 24 June 2012 - 09:31 AM
Call os.exit(), it will do the job. The reason it says "attemp to call nil" is that you are trying to call a function that does not exist, and is therefore "nil", which in Lua means "nothing"
GravityScore #3
Posted 24 June 2012 - 02:26 PM
Hi.

Thanks for the reply, but unfortunately, this does the same thing as term.exit(), it quits the program, but still displays the error "attempt to call nil"
kazagistar #4
Posted 24 June 2012 - 03:21 PM
os.exit() does not exist, and mad was wrong. There is no actually "exit" command because it does not make sense in the lua context. Whenever you run a file, you are running the contents "as if it was a function". How do you exit a function? You either let it reach the end, or you use a return statement. You can just use an empty return statement, but just remember that it has to be at the end of a block: in other words, you have to have an "end" or the end of the file right afterwards.
ardera #5
Posted 24 June 2012 - 03:40 PM
Only use
error()
it works!
(in the brackets must be nothing)
MysticT #6
Posted 24 June 2012 - 04:57 PM
Only use
error()
it works!
(in the brackets must be nothing)
There's a better way to do it in the above post. error() is not meant to end a program, it indicates that there was an error in the program.
Bossman201 #7
Posted 25 June 2012 - 12:20 AM
Thanks for the reply, but unfortunately, this does the same thing as term.exit(), it quits the program, but still displays the error "attempt to call nil"
Neither function exists.


os.exit() does not exist, and mad was wrong. There is no actually "exit" command because it does not make sense in the lua context. Whenever you run a file, you are running the contents "as if it was a function". How do you exit a function? You either let it reach the end, or you use a return statement. You can just use an empty return statement, but just remember that it has to be at the end of a block: in other words, you have to have an "end" or the end of the file right afterwards.
It is important to note that you can have multiple return calls in a function. It's useful to put them in 'if' statements and run a variable through the function to get different outputs.
MysticT #8
Posted 25 June 2012 - 12:29 AM
BTW, the reason for shell.exit() not working is because it just stops the shell loop, but doesn't end your program. You need to call shell.exit() and then end your program for it to work. Notice that it will only exit the current shell, so it won't shut down the computer if there's another one running.