Modding Help Getting "attempt to index a nil value"

Discussion in 'Starbound Modding' started by Ianus, Jan 9, 2016.

  1. Ianus

    Ianus Void-Bound Voyager

    I'm trying to read the Item-Name out of the contained Item of my Scripted-Container.

    The Error Code I get is:
    Code:
    [13:24:24.787] Error: Exception while calling script init: (LuaException) Error code 2, [string "/objects/lantian/lantian_dronerag/lantian_dro..."]:16: attempt to index a nil value (local 'contents')
    The Code is:
    Code:
    function dronesContained()
      local contents = world.containerItems(entity.id())
    
      if contents[1] then
        if contents[1].name == "lantian_drone" then
          return contents[1].count
        else
          return 0
        end
      else
        return 0
      end
    end
    Line 16 is: "if contents[1] then"

    I'm very thankful for every help.
     
  2. Errors4l

    Errors4l Spaceman Spiff

    Best Answer
    Changing that line to the following will make it skip over the code that checks contents[1] if world.containerItems() doesn't return any value.
    Code:
    if contents and contents[1] then
    Why it would return nothing I don't know. Are there items in the container?
     
    Last edited: Jan 9, 2016
  3. Ianus

    Ianus Void-Bound Voyager

    That worked perfectly, thanks!
    I already thought contents[1] would also return false if contents wouldn't return a value...
     
    Errors4l likes this.

Share This Page