[Include] OnPlayerMoneyStateChange & OnPlayerScoreStateChange - Detecting when players receive and lose money/score!
#1

Introduction:
This seems to be my first released include... Maybe this looks too simple, but may come very handy for your server!
This include will check when money is given to/taken from a player.

So let's explain something about this include:
There are 2 new callbacks:
pawn Code:
public OnPlayerMoneyStateChange(playerid, newmoney, oldmoney, bool:scripted)
public OnPlayerScoreStateChange(playerid, newscore, oldscore)

As shown above, these callbacks contain the new and the old score/money values of the player.
These callbacks get called every time the SetPlayerMoney, GivePlayerMoney, SetPlayerScore, GivePlayerScore (Read below) functions or generally when the money status of the player changes.

This include also contains one new function:
pawn Code:
GivePlayerScore(playerid, score);

This function gives score instead of setting it.

New:
OnPlayerMoneyStateChange has a new paramerer, unlike the previous version.
Now you will be able to detect if the given or lost money is called by default SA-MP functions, or gained by stunt bonuses, sprunk machines, gambling or even money hacks!
Using OnPlayerUpdate instead of a timer for better performance (Money or score could change frequently per second, so OnPlayerUpdate seemed more accurate).

Usage:
pawn Code:
public OnPlayerMoneyStateChange(playerid, newmoney, oldmoney, bool:scripted)
{
    // Printing cash informations from the callback:
    new cashdiff = newmoney-oldmoney;
    if(cashdiff < 0) cashdiff = -cashdiff;
    printf("Current Money: $%i | Old Money: $%i | %s: $%i | %s", newmoney, oldmoney, (cashdiff > 0) ? ("Received") : ("Lost"), cashdiff, (scripted) ? ("Given by script") : ("Not scripted"));

    // Checking for possible negative cash:
    new ccash = newmoney - GetPlayerMoney(playerid);
    if((ccash) < 0) printf("ID %i | Negative money: $%i", playerid, ccash);

    // Reseting money if taken by non scripted ways:
    if(!scripted)
    {
        SetPlayerMoney(playerid, oldmoney);
        new difference = newmoney - oldmoney;
        printf("ID: %s | %s $%d (Not given by script)", playerid, (difference > 0) ? ("Earned") : ("Lost"), (difference > 0) ? difference : -difference);
    }

    // Updating any textdraws:
    new _str_[30];
    format(_str_, sizeof(_str_), "Last money earned: $%i", newmoney - oldmoney);
    TestDrawSetString(MoneyTD[playerid], _str_);
    return 1;
}
pawn Code:
public OnPlayerScoreStateChange(playerid, newscore, oldscore)
{
    // Updating any textdraws, by getting rid of 0.5 - 5 second timers or OnPlayerUpdate...
    new _str_[8 + 10];
    format(_str_, sizeof(_str_), "Score: %i", newscore);
    TextDrawSetString(ScoreTD[playerid], _str_);
    return 1;
}
Installation:
  • Put msc.inc inside pawno/include folder.
  • Add this line under the defines ON EVERY SCRIPT you have:
    pawn Code:
    #include <msc>
  • Recompile your scripts!
Note:
You don't need to use these callbacks in every script, they'll work fine if used only once (Gamemode recommended).

Download: Credits:
  • wups - Inspiration from OnPlayerShootPlayer <3
  • Emmet_ - Suggested something really useful
  • Rajat_Pawar - Suggestion + pointed out a little mistake, which is now fixed!
Opinions and ratings are welcome!
Reply
#2

Nice, I like this. Very useful for anti-cheats!
Reply
#3

Nice release. These functions will be very useful for anti-cheats
Reply
#4

Perform as Anti-Cheat?
Reply
#5

Quote:
Originally Posted by Inn0cent
View Post
Perform as Anti-Cheat?
Yea, if you read it you can see how to use it too.

On topic: Good job bro, seems interesting :P.
Reply
#6

Very useful for anti cheaters.

Good job!
Reply
#7

Nice Script .
Reply
#8

People might wanna use the money/score functions in the callback, so instead of hooking the functions, maybe use a timer or perhaps OnPlayerUpdate? It'll also detect money like sprunk machines and stuff.

On the other hand, cool work.
Reply
#9

You have a major f**-up in your code, you returned SetPlayerScore in one of the redefinitions of SetPlayerMoney Copy paste disadvantage LOL Nice include, simple, instead of redefinitions, PVar is a better choice, because most people would reset the money under the callback itself!
Reply
#10

Quote:
Originally Posted by Emmet_
View Post
People might wanna use the money/score functions in the callback, so instead of hooking the functions, maybe use a timer or perhaps OnPlayerUpdate? It'll also detect money like sprunk machines and stuff.

On the other hand, cool work.
Updated! Thanks for the suggestion. Functions can now be used inside the callbacks and it's harmless to the script!
Using OnPlayerUpdate for more accuracy. Watch the main post for more info.

Quote:
Originally Posted by Rajat_Pawar
View Post
You have a major f**-up in your code, you returned SetPlayerScore in one of the redefinitions of SetPlayerMoney Copy paste disadvantage LOL Nice include, simple, instead of redefinitions, PVar is a better choice, because most people would reset the money under the callback itself!
Omg that was a silly copy-paste mistake xD Thanks for pointing that out + Fixed!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)