I feel like this is going to be a really stupid question, but I can’t figure it out.
Is there any way to get the Player object from a player ID on the server side? I have a chatMessage eventhandler, but it only returns an integer source, not the player itself.
No kidding. I wasted so much time trying to figure that out. When does an event automatically send the Player object, and when does it just send the source integer? Why are they different? Is it just something with chatMessage?
they are two way getting player id from an server event , the first one i told you , manualy send idPlayer , or another simple way where you put [source] in the serverEvent , but i never use this one but i saw a topic on the forum where someone was giving the right type , i will look for this topic after.
i didn’t understand what is different in your last message.
I have done that on one eventhandler and it works perfect.
That doesn’t work on EventHandlers[“chatMessage”] though. [FromSource] causes an error when I put it in. If I remove it, it only gives an int player source like in Lua.
This is ok since you told me how to get the player object now, but I was wondering why it didn’t work on chatMessage.
I don’t know why but if I use this method the server will give me an error, I have all the ID from the client and I can use function like API.GetPlayerName(idPlayer); work perfectly but if I try to use
PlayerList pl = new PlayerList();
Player p = pl[idplayer];
to get the player , that will give me an error. For example p.name will give an error.
I need the player not for the name but to know the position in the world, I just used the name as an example for the error.
Also using [FromSource] will give the same error :/.
When I write in the chat the command /tp ID I should move my character to the other charater using the ID, but for this I need the player and is position, so I tried to get the player and see if work Debug.WriteLine the name:
listaParole[0] = is the command
listaParole[1] = is the ID
if (listaParole[0] == "/tp")
{
//MY CHARACTER
string idPlayerIsTp = playerId.ToString();
string namePlayerIsTp = API.GetPlayerName(idPlayerIsTp);
//CHARACTER WHERE I SHOULD GO
string idPlayerWhereToGo = listaParole[1].ToString();
string namePlayerWhereToGo = API.GetPlayerName(listaParole[1]);
// IF I USE Debug.WriteLine namePlayerIsTp and namePlayerWhereToGo is working with the id and give me the name
//NOW IF I USE
PlayerList pl = new PlayerList();
Player p = pl[idPlayerWhereToGo];
Debug.WriteLine( p.Name.ToString());
//THIS WILL GIVE ME AN ERROR :.. :/
}
The error is this one on the console of the server:
Error invoking callback for event chatMessage: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at BasicResourceServer.BasicResourceServer.ControlloComandi (System.Int32 playerId, System.String playerName, System.String chatMessage) [0x00375] in <4656c8c3008d43cca7d5be68ab88c34c>:0
at BasicResourceServer.BasicResourceServer.PlayerHaScrittoInChat (System.Int32 playerId, System.String playerName, System.String chatMessage) [0x00001] in <4656c8c3008d43cca7d5be68ab88c34c>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00048] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000e7] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00008] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in <0123fd5b1a1040fe9d70a7e0d4b28acb>:0
at CitizenFX.Core.EventHandlerEntry+<Invoke>d__5.MoveNext () [0x00064] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore\EventHandlerDictionary.cs:85
if I write /tp ID but only if there is that part of code.
Is like that p doesn’t exist.
When you do pl[idPlayerWhereToGo], idPlayerWhereToGo needs to be an int networkid or string name. Since you are making idPlayerWhereToGo a string, I think it is looking for a name, not the ID. Make idPlayerWhereToGo and int before doing that so it used the correct overload function.
Solved , thx very much :P. I did that before but didn’t worked, cleaned also the chace and now worked perfectly
Player p = pl[int.Parse(listaParole[1])];
Thx very much again Briglair :D!