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

Please Help Me (lua) (error) attempt to index? ( a nil value)

Started by jay5476, 06 November 2012 - 07:28 PM
jay5476 #1
Posted 06 November 2012 - 08:28 PM
well ive tried my best to solve it now my program is in the hands of the community the error is on line 41

pastebin: www.pastebin.com/fnemsia2
ChunLing #2
Posted 06 November 2012 - 09:55 PM
I don't think that "text:len()" call works. At least, when I tried something similar I recall it not working.
Luanub #3
Posted 06 November 2012 - 09:58 PM
To get the length of text try using #text instead.
Kingdaro #4
Posted 07 November 2012 - 12:02 AM
I don't think that "text:len()" call works. At least, when I tried something similar I recall it not working.


I didn't think it worked either, but I tried it out and it's valid here.

Maybe LuaJ is different.
Lyqyd #5
Posted 07 November 2012 - 06:43 AM
I don't think that "text:len()" call works. At least, when I tried something similar I recall it not working.

This works fine as long as text contains a valid string.
Kingdaro #6
Posted 07 November 2012 - 10:35 AM
It would be so much more helpful if CC had a full traceback for errors, so we could know where the function was called in the script when it errored.

While this doesn't exist, I believe I found the problem.

op = {" 1. Log In		", " 2. Register	  ", " 3. Change Pass   ", " 4. Shutdown	  ", " 5. Force Update  ", " 6. Credits	   "}

You have six options here,


for i = 1,7 do
term.setCursorPos(1, 4+i)
if i == sel then
cPrint("X--->".. op[i] .." <---X")
else
cPrint ( op[i] )
end
end

yet you try to move through it seven times. When you call cPrint() on it, lua tries to :len() on an option that doesn't exist. To prevent future problems, you should change 7 to #op
jay5476 #7
Posted 20 November 2012 - 09:59 AM
k ty alot
billysback #8
Posted 20 November 2012 - 10:07 AM
The : works because strings are "objects" in lua, calling
string.function(string_value, x, y)
is made so that it can also be called as:
string_value:function(x, y)
as string_value is an instance of string, by calling string.function(string_value, x, y) you are basically calling:
string.function(self, x, y)
but applying self as a new instance of string,
string_value:function(x, y) is therefor equivelant to
string.function(self, x, y) as the ":" operator inserts the "self" parameter in to a function for "objects" in lua,

at least this is the explanation I believe.
I may be 100% wrong.
Kingdaro #9
Posted 20 November 2012 - 10:11 AM
You're actually perfectly right, billy. I didn't even think about that.

Though I'm confused as to why table:insert(element) doesn't work, but it works for strings. :/
Lyqyd #10
Posted 20 November 2012 - 11:31 AM
The : works because strings are "objects" in lua, calling
string.function(string_value, x, y)
is made so that it can also be called as:
string_value:function(x, y)
as string_value is an instance of string, by calling string.function(string_value, x, y) you are basically calling:
string.function(self, x, y)
but applying self as a new instance of string,
string_value:function(x, y) is therefor equivelant to
string.function(self, x, y) as the ":" operator inserts the "self" parameter in to a function for "objects" in lua,

at least this is the explanation I believe.
I may be 100% wrong.

The string type probably has an implicit metatable __index to the string library. It may even be a table, under the hood. The colon operator works for any function contained in a table, and simply passes that table as the first argument to the function.

This doesn't work for tables because tables are not initialized with an __index metatable entry, which I would posit is a good thing.
jay5476 #11
Posted 20 November 2012 - 11:46 AM
guys ive fixed it and still continuing devolping