27.03.2015, 17:37
Timers are inaccurate to track this.
A timer that runs every second might run every 1.2 seconds or something like that.
You could store the unix timestamp upon loggin in (OnPlayerConnect), you can use gettime() for that.
Store that integer in the player's account.
When you need to check the connected time, use gettime() again and substract the stored value from it.
Then you have the exact time in seconds, which can easily be converted into hours, minutes and seconds.
A timer that runs every second might run every 1.2 seconds or something like that.
You could store the unix timestamp upon loggin in (OnPlayerConnect), you can use gettime() for that.
Store that integer in the player's account.
When you need to check the connected time, use gettime() again and substract the stored value from it.
Then you have the exact time in seconds, which can easily be converted into hours, minutes and seconds.
pawn Код:
// Somewhere on top of your script
new PlayerTime[MAX_PLAYERS];
// OnPlayerConnect
public OnPlayerConnect(playerid)
{
PlayerTime[playerid] = gettime();
return 1;
}
// Helper function
ConnectedTime(playerid)
{
return (gettime() - PlayerTime[playerid]);
}
// Usage anywhere
new Msg[128];
format(Msg, sizeof(Msg), "Player %i has been connected for %i seconds", playerid, ConnectedTime(playerid));
SendClientMessageToAll(-1, Msg);