C# Get Player from ID

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.

1 Like

yes simple as : PlayerList pl = new PlayerList();

Player p = pl[idplayer];

don’t forget to send the idplayer each time to do a triggerserverevent who require an answer to the client

Thank you!!! I had a feeling it was going to be something stupidly simple like that.

stupid or smart things , when we don’t know , we can waste long time looking for it :wink:

1 Like

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.

This topic here?

But what I mean is… I trigger an event from a key press, and it automatically gives me the player object.

However, when I trigger the chat event, all it does is give me the player source integer.

Do you know why they are handled differently? Why don’t they do the same thing?

the right type for this is :

 EventHandlers["playerDropped"] += new Action<Player>(([FromSource] Player source) => { }

is the eventhandlers you talking about both from your server side ?

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.

chatmessage event is from essential mod right ? and playerdrop from fivem , thats why they are different i guess.

chatMessage is FiveM. Its triggered when you type something in chat :wink:

oh my bad ^^ i’m gonna check the code about this event later , i’m sure this answer is inside the code :wink:

[FromSource] only applies where you can use the ‘magic’ source global in Lua.

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 :/.

What exactly is your code? What do your events/handlers look like, etc? Can’t help based on the little info you gave.

1 Like

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 :.. :/

}

What is the error it gives

1 Like

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.

1 Like

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!

1 Like