1. Hi. This is my code

    Code:
    using System;
    using UnityEngine;
    using System.Linq;
    using System.Collections.Generic;namespace Oxide.Plugins
    {
        [Info("Testing", "Testing", 1.0)]
        [Description("Testing")]
        class Testing : RustPlugin
        {        void OnServerInitialized(BasePlayer player, PlayerInventory inventory)
            {
                timer.Repeat(10, 0, () =>
                {
                    if (player.inventory.GetAmount(498591726) > 2000)
                    {
                        PrintToChat("It works!");
                    }
                });
            }
        }
    }
    
    Error: Failed to run a 10.00 timer in 'Testing v1.0.0' (NullReferenceException: Object reference not set to an instance of an object)

    I have no idea what is wrong. If I remove the timer, it works.
    Thank you!!!!!!

    @Wulf
     
  2. OnServerInitialized does not have any parameters. remove the parameters and just loop through the players inside the timer.
     
  3. Hi. Sorry but I do not get it. I am really new on C#.

    Can you explain me a little more?

    Thank you for helping me!!
     
  4. OnServerInitialized() is a hook by oxide which does not provide you the parameters BasePlayer player, PlayerInventory inventory
    So you will need to loop through players instead.
    Code:
    using System;
    using System.Collections.Generic;namespace Oxide.Plugins
    {
        [Info("C4 Count Checker", "Reynostrum", 1.0)]
        [Description("Checks if somebody has more than 2k C4")]
        class Testing : RustPlugin
        {
            void Loaded()
            {
                timer.Repeat(10, 0, () =>
                {
                    foreach(BasePlayer player in BasePlayer.activePlayerList)
                    {
                        if (player.inventory.GetAmount(498591726) > 2000)
                        {
                            Puts("Player " + player.displayName + " has more than 2000 C4!");
                        }
                    }
                });
            }
        }
    }
    
    This should be working.
    I'd also use Loaded() instead of OnServerInitialized
     
  5. Wulf

    Wulf Community Admin

    OnServerInitialized() might actually be safer, as more is loaded at that point. The Loaded() hook is called before a lot of the server is loaded.
     
  6. Thanks for helping me.

    Wulf, you may help me.

    I am getting this error:
    [Error] Failed to run a 10.00 timer in 'Dupetest v1.0.0' (InvalidOperationException: Collection was modified; enumeration operation may not execute.)

    It works excellent after 1 loop. But then it stop working.
     

    Attached Files:

  7. Wulf

    Wulf Community Admin

    It's possible that a player that was in the list originally doesn't exist anymore. I would think that would throw an NRE though, but I don't see anything else it could be.
     
  8. And there is a way to fix it?

    I apologize for my ignorance.
     
  9. Wulf

    Wulf Community Admin

    You could try checking if player is null or not before trying to call it. It may be an issue with inventories not being loaded at the time your plugin was loaded too, but unsure. I'm sure someone more knowledgeable with that error could be more specific.
     
  10. I think i solved it:
    I changed: foreach(BasePlayer player in BasePlayer.activePlayerList)
    To: foreach(BasePlayer player in BasePlayer.activePlayerList.ToList())

    I tried it like 10 times and looks like its ok.