SA-MP Forums Archive
Same money for all players? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Same money for all players? (/showthread.php?tid=555670)



Same money for all players? - alanhutch - 08.01.2015

Hi.
I have inserted a system that saves how many moneys have a player in his pocket.
I wonder why all players now have the same amount of money.
Here's the code...
Код:
//OnPlayerUpdate
PlayerInfo[playerid][pSoldi] = GetPlayerMoney(playerid);
The saving/loading system is perfect...
What can I do?


Re: Same money for all players? - The__ - 08.01.2015

Have you reset the players cash when they connect?


Re: Same money for all players? - alanhutch - 08.01.2015

Yeah.
pawn Код:
ResetPlayerMoney(playerid);



Re: Skin don't save. - alanhutch - 08.01.2015

Fixed with using:
pawn Код:
for (new i = 0; i < MAX_PLAYERS; i++)
{
PlayerInfo[i][pSoldi] = GetPlayerMoney(i);
}
Now:
The skin doesen't saves...
pawn Код:
//OnPlayerUpdate
for (new i = 0; i < MAX_PLAYERS; i++)
{
PlayerInfo[i][pSkin] = GetPlayerSkin(i);
PlayerInfo[i][pSoldi] = GetPlayerMoney(i);
}
What can I do?


Re: Same money for all players? - PowerPC603 - 08.01.2015

Don't use OnPlayerUpdate for such things.
That callback is executed 30 times PER SECOND, for every player. And if they are in a vehicle, it's executed even more than 30 times per second.

And most importantly, don't loop through players, as that callback runs for every player.
Looping through all players increases the workload on your server alot and you will get lag eventually.

Your players don't change their skin that much, and their money doesn't change multiple times per second as well.

Just create a timer that runs every second, that will do fine.


You should never GET the player's money to store in a variable.
You're allowing hackers to give themselves unlimited money that way.

Create a timer that updates their money clientside by the value stored on your server, not the other way around.

This is an example, change variable-names as you see fit:
pawn Код:
// Top of your script
new PlayerTimers[MAX_PLAYERS];

// Under OnPlayerConnect
PlayerTimers[playerid] = SetTimer("YourTimer", 1000, true);

// Under OnPlayerDisconnect
KillTimer(PlayersTimers[playerid]);


forward YourTimer(playerid);
public YourTimer(playerid)
{
    // Reset the player's money and set it to the stored value in the player's account
    ResetPlayerMoney(playerid);
    GivePlayerMoney(playerid, APlayerData[playerid][PlayerMoney]);

    return 1;
}
This example will create the timers for every player and they reset the money on the client's side, then send the money stored on your server to the client.

In every case where you give the player some money, just add the value you want to APlayerData[playerid][PlayerMoney].
The timer will automatically update the money on the client, so players see it.
And if the player must pay for something, substract the value from APlayerData[playerid][PlayerMoney].

That way, even if they somehow manage to hack their client and freeze their money at $1.000.000.000 for example, the server still knows they only have $756 for example.

And before they can buy something, check if APlayerData[playerid][PlayerMoney] is equal or exceeds the price of the item they wanna buy.
If they try to buy something that costs $850, they won't be able to buy it, even if their hacked money on the client is frozen at $1.000.000.000.

They can hack all they want, but it won't help them that way.