1. Hey, so I'm trying to teleport a player when they connect to my server. So I'm using the OnPlayerConnected() hook, but getting the player is proving somewhat difficult. To test I've wrote this code inside the function:
    Code:
    void OnPlayerConnected(Network.Message packet)
            {
                Puts (BasePlayer.Find(packet.connection.userid.ToString()).userID.ToString());
            }
    When I compile and run this I get this error whenever someone connects to the server:
    Code:
    [Oxide] 1:57 PM [Error] Failed to call hook 'OnPlayerConnected' on plugin 'KingOfTheHill v1.0.0' (NullReferenceException: Object reference not set to an instance of an object)
    [Oxide] 1:57 PM [Debug]   at System.UInt64.ToString () [0x00000] in <filename unknown>:0
      at Oxide.Plugins.KingOfTheHill.OnPlayerConnected (Network.Message packet) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.KingOfTheHill.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (System.Reflection.MethodInfo method, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0
    However, when I use this there are no errors and it prints the userid without any issues.
    Code:
    void OnPlayerConnected(Network.Message packet)
            {
                Puts (packet.connection.userid.ToString());
            }
     
  2. Wulf

    Wulf Community Admin

    Pretty sure a player doesn't exist at that point, so you won't be able to find a player. The BasePlayer.Find will return true/false if a player exists or not, but in your case you are trying to check the userID of a player that doesn't exist, hence the null error.
     
  3. Oh alright, is there a way to get the players object from the userid?
     
  4. Wulf

    Wulf Community Admin

    You'd need the player to exist first, so you'd need to check in a hook where the player exists such as OnPlayerInit.
     
  5. Oh alright thanks again Wulf!