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:
When I compile and run this I get this error whenever someone connects to the server:Code:void OnPlayerConnected(Network.Message packet) { Puts (BasePlayer.Find(packet.connection.userid.ToString()).userID.ToString()); }
However, when I use this there are no errors and it prints the userid without any issues.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
Code:void OnPlayerConnected(Network.Message packet) { Puts (packet.connection.userid.ToString()); }
Solved Having some trouble with OnPlayerConnected()
Discussion in 'Rust Development' started by Zoomafou, Oct 22, 2015.
-
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.
-
Oh alright, is there a way to get the players object from the userid?
-
Wulf Community Admin
-
Oh alright thanks again Wulf!