Jump to content




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


  • You cannot reply to this topic
5 replies to this topic

#1 Unknowntissue

  • Members
  • 15 posts

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 by Unknowntissue, 10 March 2016 - 04:22 PM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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

View PostUnknowntissue, on 09 March 2016 - 01:33 AM, said:

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 by Bomb Bloke, 09 March 2016 - 01:57 AM.


#3 Unknowntissue

  • Members
  • 15 posts

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?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 March 2016 - 06:20 AM

View PostUnknowntissue, on 09 March 2016 - 03:22 AM, said:

This is actually what i had before.

Not quite what I said - you need to ditch the ".Name". ;)

#5 Unknowntissue

  • Members
  • 15 posts

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?

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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. :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users