Fixing a KeyNotFoundException?Solved
 [ConsoleCommand("stats.reset")]
        private void ResetStatsCmd(ConsoleSystem.Arg arg)
        {
            if (!arg.IsRcon) return;
            
            GetAllPlayers().ForEach(ID => PVEStatsData.Reset(ID));
            
            PrintWarning(string.Format(msg("ResetStats"), new object[] { GetAllPlayers().Count }));


        }

I have this console command that wipes stats. When i run it in console i get these errors and im not sure why. Plz help.

Failed to call hook 'ResetStatsCmd' on plugin 'NPCKillCounter v1.0.0' (KeyNotFoundException: The given key was not present in the dictionary.)
  at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <437ba245d8404784b9fbab9b439ac908>:0
  at Oxide.Plugins.NPCKillCounter+PVEStatsData.Reset (System.UInt64 id) [0x00034] in <80ffc568a10b458fb027fc939681360a>:0
  at Oxide.Plugins.NPCKillCounter.<ResetStatsCmd>m__0 (System.UInt64 ID) [0x00000] in <80ffc568a10b458fb027fc939681360a>:0
  at System.Collections.Generic.List`1[T].ForEach (System.Action`1[T] action) [0x00024] in <437ba245d8404784b9fbab9b439ac908>:0
  at Oxide.Plugins.NPCKillCounter.ResetStatsCmd (ConsoleSystem+Arg arg) [0x00012] in <80ffc568a10b458fb027fc939681360a>:0
  at Oxide.Plugins.NPCKillCounter.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x001f8] in <80ffc568a10b458fb027fc939681360a>:0
  at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <d09a1f46ca2f4432811bcfe45ad13c7b>:0
  at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <cf88a28c7fb44d36890d85a78331cc9d>:0
  at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in <cf88a28c7fb44d36890d85a78331cc9d>:0
public List<ulong> GetAllPlayers()
        {
            List<ulong> PlayersID = new List<ulong>();
            covalence.Players.All.ToList().ForEach(IPlayer => PlayersID.Add(ulong.Parse(IPlayer.Id)));
            return PlayersID;
        }



internal static void Reset(ulong id)
            {
                PVEStatsData data = Interface.Oxide.DataFileSystem.ReadObject<PVEStatsData>($"NPCKillCounter/{id}");

                if (data == null) return;

                data = new PVEStatsData();
                data.Save(id);
                Cachedplayerstats[id].heavyKills = 0;
                Cachedplayerstats[id].scarecrowkills = 0;
                Cachedplayerstats[id].scientistkills = 0;
                Cachedplayerstats[id].murdererkills = 0;

            }

thought these might help.



Merged post

But the command still works, it wipes the data, but still gives an error in console. Why is this?
add condition
if (!Cachedplayerstats​.ContainsKey(id)) {
   Cachedplayerstats[id] = new *your object*();
} 
before
Cachedplayerstats[id].heavyKills = 0;​


Merged post

If such problems arise, then perhaps you need to learn the language first?
the error says that the key was not found in the dictionary.
5df88d179ce26.jpg AVOCoder
add condition
if (!Cachedplayerstats​.ContainsKey(id)) {
   Cachedplayerstats[id] = new *your object*();
} 
before
Cachedplayerstats[id].heavyKills = 0;​


Merged post

If such problems arise, then perhaps you need to learn the language first?
the error says that the key was not found in the dictionary.

I saw that it said a key was missing, but it just confused me as to why, because the command was working fine yesterday and i haven't really changed anything, and the command is still working as intended.

5df88d179ce26.jpg AVOCoder
add condition
if (!Cachedplayerstats​.ContainsKey(id)) {
   Cachedplayerstats[id] = new *your object*();
} 
before
Cachedplayerstats[id].heavyKills = 0;​


Merged post

If such problems arise, then perhaps you need to learn the language first?
the error says that the key was not found in the dictionary.

btw, when i try this, all i get is:
Error while compiling: NPCKillCounter.cs(394,18): error CS0103: The name `CachedplayerstatsÔÇï' does not exist in the current context

you remind me of my lead developer =) he also thinks that he is programming, and I keep my hand on my face.
seriously, your problem is not knowing the language, no more
nvm, fixed it
Gj dude. It's cool to see dudes doing what you're doing.
Locked automatically