Get player id in server

Hey, I’m trying to get a player’s network id in the server script. How can I achieve this? I’ve been looking everywhere. I can only find steam, ip, and license in the Player#Identifiers.

SOLVED by using a simple client.lua script. Please FiveM, make a default “playerConnected” event on the server-side.

1 Like

If you are triggering a server event from the client its passed as ‘source’?

1 Like

when you say ‘network id of a player’, do you mean the ‘network handle’ for the player’s PED? You’ll need to get that from a client script, and send it to the server using an event.

If you mean the server ID (starting at 1 for the 1st player to join, which then increments for each player to join until the server restarts) then like @xander1998 said, just get the id from the source attribute or loop through all players and figure out which one is the player you’re looking for.

In C#, the “source” is a string? Shouldn’t it be an int? And yes, this is the id i’m looking for.

EDIT: Actually, i think i know what i’m looking for. It’s [FromSource]int id right? Im gonna test it.

1 Like

For some reason it is… if you do Player.Handle.

For triggered events, you want to do [FromSource] Player ply. Will return the player object, not just the ID (for most events).

What do you need the id for exactly? Does it have to be an int? or do you want to trigger an event for a specific source/player? (if the latter, you don’t need to have the id as an int)

Well, I’ve created a player wrapper, which will have to store the id because inside this wrapper there is a method called #GetCitizenPlayer() which uses this network id to get the player represention citizen has. Here’s my method:

/// <summary>
/// Gets Citizen's representation of player
/// </summary>
/// <returns>CitizenX's player</returns>
public Player GetCitizenPlayer() {
    // Find player in list
    Player player = new PlayerList()[this.NetId];

    // Never is null, so make sure it's a player by checking ping
    if (player.Ping == 0) return null;
    else return player;
}

The reason I’m checking for ping is because that it doesn’t giving an invalid network id does not return a null object, but an object with empty fields such as the ping being 0.

Update:
using [FromSource] int id does not work.

I think the source is a string, but it only contains a number. So you can probably just do:

int playerId = int.Parse(<player>.Handle);

You need to use:

[FromSource] Player playerSrc

(This is of course when you’re receiving an event from that player)

1 Like

Now, this I have tried. And it works, most of the time. However, in the “playerConnected” event (I want to set the player’s network id when they log in), the Handle returns a rather random large number such as “142342” instead of “3” as it normally does.

This is why I’ve tried looking into a server event that is fired when the player is fully connected, so the handle will actually be the assigned network id, but have not found such an event.

Also, the “[FromSource] Player playerSrc” works, however, the Player object does not hold the network id (it does hold a Player#Handle, but as stated earlier, this does not satisfy me during the playerConnecting process.

Example from vMenu:

// in the constructor of the class:
EventHandlers.Add("vMenu:SummonPlayer", new Action<Player, int>(SummonPlayer));
// SummonPlayer function


/// <summary>
/// Teleport a specific player to another player.
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
private void SummonPlayer([FromSource] Player source, int target)
{
    if (IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.Summon") || IsPlayerAceAllowed(source.Handle, "vMenu.Everything") ||
        IsPlayerAceAllowed(source.Handle, "vMenu.OnlinePlayers.All"))
    {
        // Trigger the client event on the target player to make them teleport to the source player.
        Player targetPlayer = new PlayerList()[target];
        if (targetPlayer != null)
        {
            TriggerClientEvent(player: targetPlayer, eventName: "vMenu:GoToPlayer", args: source.Handle);
            return;
        }
        TriggerClientEvent(player: source, eventName: "vMenu:Notify", args: "An unknown error occurred. Report it here: vespura.com/vmenu");
    }
    else
    {
        BanManager.BanCheater(new PlayerList()[target]);
    }
}
#endregion

(note this is not when the player connects, just an example on the FromSource stuff)

Well, yes, but as you can see this is a custom event, and the id is already being passed through the constructor, which is got through the custom event data.

This is an example from playerConnecting:

// inside constructor
EventHandlers.Add("playerConnecting", new Action<Player, string, CallbackDelegate>(CheckForBans));

// somewhere below: the function
private void CheckForBans([FromSource]Player source, string playerName, CallbackDelegate kickCallback){/*...*/}

When connecting, they are assigned a temp ID (the large number).

To my knowledge, there is no such event. To detect this, I use the following (on the client), which only triggers when player first joins:



        public async Task OnTick()
        {
            if (!IsPlayerSessionStarted && API.NetworkIsSessionStarted() && !API.GetIsLoadingScreenActive())
            {
                IsPlayerSessionStarted = true;
                TriggerServerEvent("br_ply:joined");
            }
        }

Is this done through the server? Because I don’t want a client script for this, I feel like it’s too simple.

Vespura, it still doesn’t help me very much :rofl:

This example is all server side:

what @Briglair sent was client side.

Not sure what you’re trying to do then? I thought you wanted to get the player id on the server?

I’m trying to get the network id to the player that is connecting. But as @Briglair just mentioned, the player have not been assigned its network id yet.

I feel like it’s kinda weird though that FiveM does not offer any event that is fired when the player’s connection have been successfully established.

He’s looking for an event that is triggered when fully connected so he can get the proper player handle. The playerConnecting event only gives the temp ID.

You could always use playerSpawned and have a bool checking if it is first spawn or not. However, an issue would arise if you are resetting the script (for testing/debugging) as you will need to respawn to trigger it. The code I posted above (with client portion) avoids that and will always trigger when initialized.

EDIT: I think playerSpawned is actually client side, too.

Yeah, that wouldn’t exactly be ideal.

I feel like having a client script for this is overkill. I mean, the server should know when a connection with a player have successfully been established and the network id (Handle) have been assigned. :angry: :angry: :angry:

EDIT: And it defiantly does, it just doesn’t expose this information to us which makes me angry. I feel like too many people would have a use for this to ignore it.

Fair enough, though yeah there’s no way to get that from the server, you could loop through new PlayerList() and check for new players there manually, though that’s obviously not the best solution, you should probably just use a client script that will trigger a server event once the player is fully joined.