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