Save player help
#1

Hello!

I have a Save Player function in my gamemode but sometimes I have to restart the server. If there are players online playing at the time im restarting the server, they are saved in coord 0,0,0. I suppose that the OnPlayerDisconnect is not called when this happens or something (because I have the function in OnPlayerDisconnect) or the function calls after the player disconnect because it sets the coords X,Y,Z to 0.

What should I do, a timer for save the players coords every x seconds or there is any way to solve this? Here are the code:

Код:
public OnPlayerDisconnect(playerid, reason)
{
	if (gLogged[playerid] == 1)
	{
		if (PlayerInfo[playerid][pPrision] <= 0)
		{
			SavePlayer(playerid);
			ResetPlayerVarsGM(playerid);
  		}
	}
	return 1;
}

public SavePlayer(playerid)
{
	new Float:jX, Float:jY, Float:jZ, Float:hp, Float:armour, dmoney, playerskin;
	GetPlayerPos(playerid, jX, jY, jZ);
	GetPlayerHealth(playerid, hp);
	GetPlayerArmour(playerid, armour);
	dmoney= GetPlayerCash(playerid);
	playerskin = GetPlayerSkin(playerid);

	PlayerInfo[playerid][pSkin] = playerskin;
	Update(playerid, PlayerInfo[playerid][pSkin]);

	PlayerInfo[playerid][pCash] = dmoney;
	Update(playerid, pCashu);

	PlayerInfo[playerid][pX] = jX;
	Update(playerid, pXu);

	PlayerInfo[playerid][pY] = jY;
	Update(playerid, pYu);

	PlayerInfo[playerid][pZ] = jZ;
	Update(playerid, pZu);

	PlayerInfo[playerid][pVida] = hp;
	Update(playerid, pVidau);

	PlayerInfo[playerid][pArmadura] = armour;
	Update(playerid, pArmadurau);

	PlayerInfo[playerid][pInterior] = GetPlayerInterior(playerid);
	Update(playerid, pInterioru);

	PlayerInfo[playerid][pVW] = GetPlayerVirtualWorld(playerid);
	Update(playerid, pVWu);

 	AccountInfo[playerid][aOnline] = 0;
	UpdatePlayerAccount(playerid, aOnlineu, AccountInfo[playerid][aName]);

	return 0;
}
Reply
#2

GetPlayer* functions do NOT work in OnPlayerDisconnect because the client has already disconnected at that point.

Also to close the server gracefully you should use RCON exit. Don't kill the process directly.
Reply
#3

Yes but if i disconnect normally, the variables are saved. Checking the wiki:

Код:
public OnPlayerDisconnect(playerid, reason)
{
    new
        szString[64],
        playerName[MAX_PLAYER_NAME];
 
    GetPlayerName(playerid, playerName, MAX_PLAYER_NAME);
 
    new szDisconnectReason[3][] =
    {
        "Timeout/Crash",
        "Quit",
        "Kick/Ban"
    };
 
    format(szString, sizeof szString, "%s left the server (%s).", playerName, szDisconnectReason[reason]);
 
    SendClientMessageToAll(0xC4C4C4FF, szString);
    return 1;
}
The function GetPlayerName works because it sends a message to all players saying that 'x' user has disconnected.
Reply
#4

I guess Vince meant, you have to put your saving code in OnPlayerDisconnect
Quote:
Originally Posted by Vince
Посмотреть сообщение
Also to close the server gracefully you should use RCON exit. Don't kill the process directly.
Hm, I was having a similar issue, I guess you answered my issue.
Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
GetPlayer* functions do NOT work in OnPlayerDisconnect because the client has already disconnected at that point.
That's not true at all, i'm saving my position in OnPlayerDisconnect on my gamemode and it works as well...

Btw, does this Update function save the data in a file?
Also try to put
Код:
gLogged[playerid] = 0;
in OnPlayerConnect.
Reply
#6

PHP код:
new S_Timer 10// every 10 seconds
public OnPlayerConnect(playerid)
{
        
SetTimerEx("SavePlayer"S_Timer*1000true"i"playerid); // "true" timer will repeat every "S_Timer" Seconds
        
return true;
}
public 
SavePlayer(playerid)
{
    new 
Float:jXFloat:jYFloat:jZFloat:hpFloat:armourdmoneyplayerskin;
    
GetPlayerPos(playeridjXjYjZ);
    
GetPlayerHealth(playeridhp);
    
GetPlayerArmour(playeridarmour);
    
dmoneyGetPlayerCash(playerid);
    
playerskin GetPlayerSkin(playerid);
    
PlayerInfo[playerid][pSkin] = playerskin;
    
Update(playeridPlayerInfo[playerid][pSkin]);
    
PlayerInfo[playerid][pCash] = dmoney;
    
Update(playeridpCashu);
    
PlayerInfo[playerid][pX] = jX;
    
Update(playeridpXu);
    
PlayerInfo[playerid][pY] = jY;
    
Update(playeridpYu);
    
PlayerInfo[playerid][pZ] = jZ;
    
Update(playeridpZu);
    
PlayerInfo[playerid][pVida] = hp;
    
Update(playeridpVidau);
    
PlayerInfo[playerid][pArmadura] = armour;
    
Update(playeridpArmadurau);
    
PlayerInfo[playerid][pInterior] = GetPlayerInterior(playerid);
    
Update(playeridpInterioru);
    
PlayerInfo[playerid][pVW] = GetPlayerVirtualWorld(playerid);
    
Update(playeridpVWu);
     
AccountInfo[playerid][aOnline] = 0;
    
UpdatePlayerAccount(playeridaOnlineuAccountInfo[playerid][aName]);
    return 
1// why return false ? returning false on commands or functions that means it's not success excuted

but if u using dini/ outdated system That will make some lags
Reply
#7

Quote:
Originally Posted by Yaa
Посмотреть сообщение
PHP код:
new S_Timer 10// every 10 seconds
public OnPlayerConnect(playerid)
{
        
SetTimerEx("SavePlayer"S_Timer*1000true"i"playerid); // "true" timer will repeat every "S_Timer" Seconds
        
return true;
}
public 
SavePlayer(playerid)
{
    new 
Float:jXFloat:jYFloat:jZFloat:hpFloat:armourdmoneyplayerskin;
    
GetPlayerPos(playeridjXjYjZ);
    
GetPlayerHealth(playeridhp);
    
GetPlayerArmour(playeridarmour);
    
dmoneyGetPlayerCash(playerid);
    
playerskin GetPlayerSkin(playerid);
    
PlayerInfo[playerid][pSkin] = playerskin;
    
Update(playeridPlayerInfo[playerid][pSkin]);
    
PlayerInfo[playerid][pCash] = dmoney;
    
Update(playeridpCashu);
    
PlayerInfo[playerid][pX] = jX;
    
Update(playeridpXu);
    
PlayerInfo[playerid][pY] = jY;
    
Update(playeridpYu);
    
PlayerInfo[playerid][pZ] = jZ;
    
Update(playeridpZu);
    
PlayerInfo[playerid][pVida] = hp;
    
Update(playeridpVidau);
    
PlayerInfo[playerid][pArmadura] = armour;
    
Update(playeridpArmadurau);
    
PlayerInfo[playerid][pInterior] = GetPlayerInterior(playerid);
    
Update(playeridpInterioru);
    
PlayerInfo[playerid][pVW] = GetPlayerVirtualWorld(playerid);
    
Update(playeridpVWu);
     
AccountInfo[playerid][aOnline] = 0;
    
UpdatePlayerAccount(playeridaOnlineuAccountInfo[playerid][aName]);
    return 
1// why return false ? returning false on commands or functions that means it's not success excuted

but if u using dini/ outdated system That will make some lags
It's not needed, why should he save it every 10 seconds? And create a timer for every player? I'm thinking about it in mysql, a update for every player every 10 seconds, that should be very laggy, cpu would reach 50% or more...
He just have to find where's the part that doesn't works, cause i do it in OnPlayerDisconnect in my gamemode and it works good...
Plus, there's no checks about gLogged, it will RESET every variable if the player is not logged
(That's not for flaming or criticize, but just saying it would lag a lot)
Reply
#8

Quote:

What should I do, a timer for save the players coords every x seconds or there is any way to solve this? Here are the code:

Please Read before speak
Reply
#9

Quote:
Originally Posted by Yaa
Посмотреть сообщение
Please Read before speak
You should read, he asked if that's NEEDED, and it's not.
Why should he do it with a timer if that can work in OnPlayerDisconnect?
Reply
#10

Quote:

I have to restart the server. If there are players online playing at the time im restarting the server, they are saved in coord 0,0,0.

Please read before speak

he turn off server directly so OnPlayerDisconnect not called
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)