The exact conditions are unknown, although it always happens when the magazine amount changes (not internal magazine ammo).
Multiplayer seems to give more frequent crashes but that may be just an observation error.
Fix was actually developed by me and my friend, in libs/powerup.lua around line 25, func powerup.restoreAmmo:
function powerup.restoreAmmo(player, powerupSettings)
local ammoData = powerupSettings["ammo"]
if not ammoData then
return false
end
local character_ammo = player.get_inventory(defines.inventory.character_ammo)
if not character_ammo then
return false
end
local ammoRestored = false
-- Only iterate up to the smaller of the two arrays
local maxIndex = math.min(#character_ammo, #ammoData)
for i = 1, maxIndex do
-- Check if the current inventory slot is valid
if character_ammo[i].valid_for_read then
-- Make sure ammoData[i] exists before trying to access it
if ammoData[i] and ammoData[i].name == character_ammo[i].name then
if (character_ammo[i].count+1) == ammoData[i].count then
character_ammo[i].count = ammoData[i].count
ammoRestored = true
break
end
end
end
end
return ammoRestored
end
You basically want to check if ammoData[i] even exists, because for some unknown for me reason it can be empty.
Also its good if you just iterate to the smaller index rather than simply character_ammo, and this also can be one of the reasons it crashes.
If you need further explaination or just around-help you can add me on dc: donhui5680