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

[Solved] Keep getting " attempt to index ? (a nil value)

Started by Unknowntissue, 09 March 2016 - 12:33 AM
Unknowntissue #1
Posted 09 March 2016 - 01:33 AM
the code that I am working with. http://pastebin.com/Fz6hxbuA

I think the problem is that there is something that walks into the range of the sensor that doesn't have a "Name" key.

The program actually runs for a while before i even get this error so it must be something that happens only sometimes. I think i need to find a way to protect the program when this happens.

the problem happens on line 24 according to the error code i get.

Please let me know if I need to clear some things up for you in my code. I am completely self taught so I don't really know if i am following good programming practices.
Edited on 10 March 2016 - 03:22 PM
Bomb Bloke #2
Posted 09 March 2016 - 01:54 AM
Rather, the error is saying that "moreDetails" isn't a table, but instead it's nil - so you can't index into it to look for a "Name" key in the first place.

mySensor.getTargets() and mySensor.getTargetDetails(i) each take a moment to execute, so it's possible for the scanner script to detect a creature in range when searching for targets, and for that creature to move out of range before it gets around to querying for details.

I'd change this:

    if not moreDetails.Name then

    else

… to this:

    if moreDetails then

Please let me know if I need to clear some things up for you in my code. I am completely self taught so I don't really know if i am following good programming practices.

You could localise "mySensor", as well as your two functions. Otherwise things're looking pretty solid.
Edited on 09 March 2016 - 12:57 AM
Unknowntissue #3
Posted 09 March 2016 - 03:22 AM

if moreDetails.Name then

This is actually what i had before. I was still getting the same problem for some reason.

Your comment did just gave me the thought that i should be checking for the nil before it gets to line 24. Do you think that will fix the problem?
Bomb Bloke #4
Posted 09 March 2016 - 06:20 AM
This is actually what i had before.

Not quite what I said - you need to ditch the ".Name". ;)/>
Unknowntissue #5
Posted 09 March 2016 - 04:47 PM
Ahhhh alright I think that will do it. Thanks for the help! This there something i have to do once an issue is solved? Like edit the op so it says solved or something?
Bomb Bloke #6
Posted 09 March 2016 - 11:50 PM
Changing the thread title's indeed good practise - use the Full Editor for that. If you get stuck, just posting a regular "fixed!" reply is good enough. :)/>