Page 1 of 1

Armor Inventory Problem

Posted: Tue Aug 30, 2016 6:27 pm
by kiba

Code: Select all

function getArmor(index)
  local armor = game.players[index].get_inventory(defines.inventory.player_armor)[1]
  return armor
end

function armorCheck(index)
  local armor = getArmor(index)
  if armor.grid ~= nil then
    return true
  end
  return false
end

script.on_event(defines.events.on_player_armor_inventory_changed, function(event)
  local index = event.player_index
  if armorCheck(index) then
    local armor = getArmor(index)
    local equipment = armor.grid.equipment
    for i, e in ipairs(equipment) do
      if e.name == "hoverboard" then
        activateEquipment(index)
      end
    end
  else
    deactivateEquipment(index)
  end
end)
Error:

Code: Select all

Error while running event on_player_armor_inventory_changed (ID 35)
LuaItemStack API call when LuaItemStack was invalid.
stack traceback:
	__MagneticFloor__/src/armor.lua:8: in function 'armorCheck'
	__MagneticFloor__/src/armor.lua:16: in function <__MagneticFloor__/src/armor.lua:14>
I don't understand what's going on when I removed a power armor item. I believe I got a valid ItemStack, but then it doesn't let me do anything or tell me if it's empty.

If I do anything like ItemStack.count or ItemStack.name, it gives me the same error.

Re: Armor Inventory Problem

Posted: Tue Aug 30, 2016 8:46 pm
by Rseding91

Re: Armor Inventory Problem

Posted: Tue Aug 30, 2016 8:46 pm
by Rseding91
Also, get_inventory(defines.inventory.player_armor) can return nil if the player has no character so you'll need to check for nil on that first.

Re: Armor Inventory Problem

Posted: Wed Aug 31, 2016 10:38 pm
by kiba
Thanks. I spent about hour or so trying to fix it before giving up and asking for help. Wish I have done earlier, but I have a tendency to try to try everything myself first before asking for help.