[Help] [C#] Passing "Player" from server to client

I am unable to pass “Player” as an argument using TriggerClientEvent.
Here is a fragment of server code, the function OnPlayerKilled triggers properly, but the TriggerClientEvent does not when used like this:

public void OnPlayerKilled([FromSource] Player deadPlayer, int killerID)
{
    if (deadPlayer != null)
    {
         Player killer = Players[killerID];
         TriggerClientEvent(killer, "onAssasinate", deadPlayer);
     }
}

When I do not pass any arguments or try something else (like string) the TriggerClientEvent works.
Also - both killer and deadPlayer point to the right players inside the “OnPlayerKilled” function(I tested it and printed their names inside this function).

So why is it that I cannot pass Player to my client script?
Is it just impossible? If so, can I get ID from this Player and pass it as int to client somehow?




Edit:
Answer to the problem - this is the function that returns the ID of target Player (on serverside), this ID should be passed as parameter instead of the whole Player object:

private int GetIDFromPlayer(Player target)
{
    PlayerList pl = new PlayerList();
    int ID = 0;
    foreach (Player p in pl)
    {
        if (p.Handle == target.Handle)
        {
            return ID;
        }
        ID++;
    }
    return -1;
}
1 Like

I don’t think you can pass complex objects in events from server to client. Instead, you should be able to just pass the player’s ID.

I believe you get the ID by doing deadPlayer.ID (or something similar). Have a look in VisualStudio, it should give you a list of properties and methods (with intellisense) in a drop down when you put the period after deadPlayer.

1 Like

That’s the problem, I am not sure how to retrieve the ID. The closest thing to ID that Visual Studio prompter suggests is:

deadPlayer.Identifiers

that returns IdentifierCollection object. There is no ID inside it though.

no man , nothing more easy then get the ID from both side , it’s just you have to look at different place

from client : Game.Player.ServerId

from Serveur : PlayerList pl = new PlayerList();
for get the player by id : Player p = pl[idserver];

as @Havoc said , it’s better to just pass one param instead of whole object who can be heavy and where you certainly only need one or 2 things from it.

and sometime passing the identifier can be a good solution aswell, from the identifier you can get player aswell with the playerlist.

and last thing , the object Player from server and the object player from client are different object

Ok, I get it, but is there a simple way to get ID from player (and not the other way around) in server script?
Do I have to create a PlayerList, loop through all the players and check whether certain player equals the one I want and then return the incremented identifier variable?

from server you can simply use the [FromSource] on event for get the player who trigger this event. otherwise if you have to act on another player then the one who trigger the server , then yes you have to use PlayerList but no need to loop the tab you give him directly the serverId like this Player p = new PlayerList()[idServer];

1 Like

The thing is that I have the Player object and need to extract his ID to send as an event parameter to another client.
Let me write my problem in code:

public void OnPlayerKilled([FromSource] Player deadPlayer, int killerID)
{
    Player killer = Players[killerID];
    //Here I need to transform "Player deadPlayer" into "int ID" in order to send
    //information about deadPlayer to killer
    TriggerClientEvent(killer, "onAssassinate", ID);
}

And now I have tried doing a separate function like this:

private int GetIDFromPlayer(Player target)
{
    PlayerList pl = new PlayerList();
    int ID = 0;
    foreach (Player p in pl)
    {
        if (p == target)
        {
            return ID;
        }
        ID++;
    }
    return -1;
}

But it doesn’t do good job at comparing if (p == target) as it always returns -1




EDIT: I changed

if (p == target)

to

if (p.Handle == target.Handle)

and it seems to be working fine for now.

Thanks @304bl and @Havoc for your input, it was really helpful for me.

4 Likes