I haven't messed much with it, but here it is in your code:
if capsule.capsule_action.attack_parameters and capsule.capsule_action.attack_parameters.ammo_type.action.action_delivery.projectile then
You could simply change this line to this to avoid the problem:
if capsule.capsule_action and capsule.capsule_action.attack_parameters and capsule.capsule_action.attack_parameters.ammo_type and capsule.capsule_action.attack_parameters.ammo_type.action and capsule.capsule_action.attack_parameters.ammo_type.action.action_delivery and capsule.capsule_action.attack_parameters.ammo_type.action.action_delivery.projectile then