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

Coding Problem: Attempt to call nil error

Started by Freydoo, 01 March 2014 - 10:02 AM
Freydoo #1
Posted 01 March 2014 - 11:02 AM
I get an "attempt to call nil" error in line 9, 11, 12. It is also saying that it expected a number in l.9 but that doesn't work as well. Please help, I can't figure out what to do.

Pastebin link: http://pastebin.com/sdPdhtwS
CometWolf #2
Posted 02 March 2014 - 11:52 AM
monitors do not have a function called print, only write. as for the other lines, you probably don't even have a monitor connected to the back. and numerical colors do work, it is the only thing that works. they're easily acessible through the colors api though.

colors.blue
colors.red
colors.green
Bomb Bloke #3
Posted 02 March 2014 - 12:10 PM
Note that if you construct a string with the content "colors.green", that's not the same thing as referring to the variable with the name of "colors.green".

Instead of:

"colors."..io.read()

… you could use:

colors[io.read()]

(Bear in mind the lack of quotation marks is fully intentional.)

Without going in to too much detail as to why that works, suffice to say that in most cases getting users to type in variables isn't such an easy option, and is very seldom a "good" option. For example, in this case if the user doesn't type the name of a valid colours API colour it'll crash rather messily, so you're really better off storing what they type in a variable, checking if it's valid, then only actually going ahead with the text colour change if it is.

Another point, if your monitor is connected to the back of your computer via a wired modem, then the modem will be assigned the name of "back" and the monitor will be named something like eg "monitor_0". The actual name appears on-screen whenever you activate / de-activate the modem (by right-clicking it).
Freydoo #4
Posted 03 March 2014 - 11:33 AM
Thank you very much for your help! :)/>