EventHandlers["playerShowInfo"] += new Action<Player>(ShowPlayerInfo);
private static void ShowPlayerInfo([FromSource]Player player)
{
Vector3 playerCoords1 = player.Character.Position;
Vector3 playerCoords2 = player.Character.Rotation;
Vector3 playerCoords3 = GetEntityCoords(player.Character.Handle);
Debug.WriteLine($"{playerCoords1}"); // ALWAYS WRONG (X: 0, Y: 0, Z:0)
Debug.WriteLine($"{playerCoords2}"); // ALWAYS WRONG (X: 0, Y: 0, Z:0)
Debug.WriteLine($"{playerCoords3}"); // ALWAYS WRONG (X: 0, Y: 0, Z:0)
Debug.WriteLine($"{player.Name}"); // OK
}
Not sure if I’m doing something wrong as well, but sometimes this occurs for me too. Last time I noticed that restarting FiveM client (not just disconnecting!) and then rejoining helped.
My initial issue was that I noticed GetEntityHealth(player.Handle) returned always zero. This messed up my game mode as server tought the player was dead. Then I noticed that coords and heading was also wrong, but Name was still correct (just like in your case).
I just wrote this code a minute a go to check if player is in this state and then drop it from server with notice to restart FiveM:
private Dictionary<string, float> _desyncTimer = new Dictionary<string, float>();
private async Task OnCheckUnsyncPlayers()
{
await Delay(1000);
var playerList = Players;
foreach (var player in playerList)
{
if (player == null || player.Handle == null || player.Character == null) continue;
if (!_desyncTimer.ContainsKey(player.Handle))
{
_desyncTimer[player.Handle] = 0;
}
// if player entity stays zero health for too long, consider it as being desync
// this has occurred few times and it ruins game mode as players coords and health is not correct
if (GetEntityHealth(player.Character.Handle) <= 0)
{
_desyncTimer[player.Handle] += 1000;
if (_desyncTimer[player.Handle] > 20000)
{
DropPlayer(player.Handle, "Please RESTART FiveM and rejoin (Reason: Desync)");
}
}
else
{
_desyncTimer[player.Handle] = 0;
}
}
}
Edit:
Just use it like this:
Tick += OnCheckUnsyncPlayers;
Would be great if some one could comment what could cause this in the first place…