Player is null when attacking patrol helicopterSolved

For some reason, When ever I attack patrol helicopter i get Player is "Null in console" but also I get "Found player". I don't understand how it is null as the entity dealing damage to the heli is a player.

When the heli gets taken down i get this

(16:08:13) | Exception while calling NextTick callback (NullReferenceException: Object reference not set to an instance of an object)
  at Oxide.Plugins.DamageNotifier+<OnEntityTakeDamage>c__AnonStorey0.<>m__0 () [0x0000c] in <59e775c7db404fafb133bf6821524798>:0 
  at Oxide.Core.OxideMod.OnFrame (System.Single delta) [0x00051] in <50629aa0e75d4126b345d8d9d64da28d>:0
 public Dictionary<BasePlayer, int> HeliHits = new Dictionary<BasePlayer, int>();
        object OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
        {
            if (entity.PrefabName == "patrolhelicopter" || entity.PrefabName == "patrol_helicopter" || entity.ShortPrefabName == "patrol_helicopter" || entity.ShortPrefabName == "patrolhelicopter")
            {
                // Puts("Heli Hit"); 
                NextTick(() =>
                {
                    BasePlayer player = info.InitiatorPlayer;
                    ItemDefinition itemDefinition = info.Weapon.GetEntity().GetComponent<BaseProjectile>().primaryMagazine.ammoType;
                    if (player != null)
                    {
                        if (itemDefinition.shortname != "ammo.rifle") { Puts("Not rifle ammo"); return; }

                        int damages = (int)info.damageTypes.Total();
                        if (!HeliHits.ContainsKey(info.InitiatorPlayer))
                        {
                            HeliHits.Add(info.InitiatorPlayer, damages);
                        }
                        else
                        {
                            HeliHits[info.InitiatorPlayer] += damages;
                        }
                        Puts(Found Player);
                    }
                    Puts("Player is Null"); return;
                });
            }
            return null;
        }​

you do not return in the if so the code Player is Null will run in ANY case

Qble9YPyseIOkyH.png misticos

you do not return in the if so the code Player is Null will run in ANY case

omg idk how i didn't see that little mistake, thanks for pointing that out

However I'm still getting this error when the entity dies: 

Exception while calling NextTick callback (NullReferenceException: Object reference not set to an instance of an object)
  at Oxide.Plugins.DamageNotifier+<OnEntityTakeDamage>c__AnonStorey0.<>m__0 () [0x0000c] in <1436c2eb854b4d1ba02bd14a4e088e99>:0 
  at Oxide.Core.OxideMod.OnFrame (System.Single delta) [0x00051] in <50629aa0e75d4126b345d8d9d64da28d>:0

Also now this hook here does not seem to work as it's not printing anything to console and is not running anything.

 void OnEntityDeath(PatrolHelicopterAI heli, HitInfo info)
        {
            if (info == null) { Puts("null"); return; }
            StringBuilder result = new StringBuilder();
            Puts("Death");
            foreach (KeyValuePair<BasePlayer, int> hitInfo in HeliHits.OrderByDescending(x => x.Value).Take(1)) // Get players with their score ordered from the best to the worst
            {
                BasePlayer player = hitInfo.Key;

                int DamageDone = hitInfo.Value;

                result.Append($"{player.displayName} : {DamageDone} \n");
                Server.Broadcast(result.ToString());
            }
        }​

the error is still in OnEntityTakeDamage, the NextTick code. please show the new code

My bad. Here is the code:

object OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
        {
            if (entity.PrefabName == "patrolhelicopter" || entity.PrefabName == "patrol_helicopter" || entity.ShortPrefabName == "patrol_helicopter" || entity.ShortPrefabName == "patrolhelicopter")
            {
                Puts("Heli Hit"); 
                NextTick(() =>
                {
                    BasePlayer player = info.InitiatorPlayer;
                    ItemDefinition itemDefinition = info.Weapon.GetEntity().GetComponent<BaseProjectile>().primaryMagazine.ammoType;
                    if (player != null)
                    {
                        if (itemDefinition.shortname != "ammo.rifle") { Puts("Not rifle ammo"); return; }

                        int damages = (int)info.damageTypes.Total();
                        if (!HeliHits.ContainsKey(info.InitiatorPlayer))
                        {
                            HeliHits.Add(info.InitiatorPlayer, damages);
                        }
                        else
                        {
                            HeliHits[info.InitiatorPlayer] += damages;
                        }
                        Puts("Found player"); 
                    }
                    else Puts("Player is Null"); return; 
                });
            }
            return null;
        }

I"m not sure that you'd need the NextTick, but this should resolve some of your issues:

        void OnEntityTakeDamage(PatrolHelicopterAI entity, HitInfo info)
        {
            BasePlayer player = info.InitiatorPlayer;
            ItemDefinition itemDefinition = info.Weapon?.GetEntity()?.GetComponent<BaseProjectile>()?.primaryMagazine.ammoType;
            if (player != null && itemDefinition != null)
            {
                if (itemDefinition.shortname != "ammo.rifle")
                {
                    Puts("Not rifle ammo");
                    return;
                }

                int damages = (int)info.damageTypes.Total();
                if (!HeliHits.ContainsKey(info.InitiatorPlayer))
                {
                    HeliHits.Add(info.InitiatorPlayer, damages);
                }
                else
                {
                    HeliHits[info.InitiatorPlayer] += damages;
                }
            }
        }​

Thank you for replying, however now nothing is happening. I tried to add a Puts(); at the very start and it doesn't even shows up. I noticed that you made it a void instead of an object, I'm not sure if this has anything to do with why its not working as the documents say it's an object. I tried making it an object and it still does nothing. There are no errors when it compiles or even when the entity gets hit.
Here is my code:

        void OnEntityTakeDamage(PatrolHelicopterAI entity, HitInfo info)
        {
            Puts("Hit Test");
            BasePlayer player = info.InitiatorPlayer;
            ItemDefinition itemDefinition = info.Weapon?.GetEntity()?.GetComponent<BaseProjectile>()?.primaryMagazine.ammoType;
            if (player != null && itemDefinition != null) 
            {
                Puts("Hit"); 
                if (itemDefinition.shortname != "ammo.rifle")
                {
                    Puts("Not rifle ammo");
                    return;
                }
                Puts("Rifle Ammo");

                int damages = (int)info.damageTypes.Total();
                if (!HeliHits.ContainsKey(info.InitiatorPlayer))
                {
                    HeliHits.Add(info.InitiatorPlayer, damages);
                }
                else
                {
                    HeliHits[info.InitiatorPlayer] += damages;
                }
            }
        }​

You aren't returning anything, so the return type wouldn't matter. You could try changing back to BaseEntity and then making sure the class I changed it to is correct with a debug output.

Oh thank you it worked!

I would try to narrow down which class is used though to only have that entity called.

Locked automatically