Error while running event on_player_ammo_inventory_changed (ID 36)
__Placeholdername__/control.lua:29: attempt to index field '?' (a nil value)
Code in question:
Code: Select all
script.on_event(defines.events.on_player_ammo_inventory_changed, function(event) onChange(event) end)
global.ammorack = {}
function check ()
if unexpected_condition then error() end
if player.opened.name=="nil" then end
end
function onChange(event)
local player = game.players[event.player_index]
if pcall(check) then player.print ("no error") else
local oldrifleammo = 0
local oldriflehcammo = 0
local oldpistolammo = 0
local rifleammo = 0
local riflehcammo = 0
local pistolammo = 0
player.print ("script running")
if global.ammorack[1] and global.ammorack[1][1] then oldrifleammo = global.ammorack[1][1] end
if global.ammorack[1] and global.ammorack[1][2] then oldriflehcammo = global.ammorack[1][2] end
if global.ammorack[1] and global.ammorack[1][3] then oldpistolammo = global.ammorack[1][3] end
if player.get_item_count("charged-rifle-energy-cell") == "nil" then rifleammo = 0 else rifleammo = rifleammo + player.get_item_count("charged-rifle-energy-cell") end
if player.get_item_count("supercharged-rifle-energy-cell") == "nil" then rifleammo = rifleammo + 0 else rifleammo = rifleammo + player.get_item_count("supercharged-rifle-energy-cell") end
if player.get_item_count("charged-rifle-high-capacity-cell") == "nil" then riflehcammo = 0 else riflehcammo = riflehcammo + player.get_item_count("charged-rifle-high-capacity-cell") end
if player.get_item_count("supercharged-rifle-high-capacity-cell") == "nil" then riflehcammo = riflehcammo + 0 else riflehcammo = riflehcammo + player.get_item_count("supercharged-rifle-high-capacity-cell") end
if player.get_item_count("charged-pistol-energy-cell") == "nil" then pistolammo = 0 else pistolammo = pistolammo + player.get_item_count("charged-pistol-energy-cell") end
if player.get_item_count("supercharged-pistol-energy-cell") == "nil" then pistolammo = pistolammo + 0 else pistolammo = pistolammo + player.get_item_count("supercharged-pistol-energy-cell") end
if rifleammo < oldrifleammo then player.insert{name="rifle-energy-cell", count=((oldrifleammo)-(rifleammo))} end
if riflehcammo < oldriflehcammo then player.insert{name="pistol-energy-cell", count=((oldriflehcammo)-(riflehcammo))} end
if pistolammo < oldpistolammo then player.insert{name="pistol-energy-cell", count=((oldpistolammo)-(pistolammo))} end
global.ammorack[player][1] = rifleammo
global.ammorack[player][2] = riflehcammo
global.ammorack[player][3] = pistolammo
end
end
As you can see, the script is run when the player's ammo inventory is modified. I am trying to use a multidimensional array to store the magazine count at the end of the script so it can be compared to the new contents of the players' inventory when the script is next run. The comparison checks for a difference in magazine quantity, and makes up the difference with depleted energy cells.