Simple functions doubles the amount.
#1

Hi I've ran into the most annoying bug ever. Tried to fix it for 3 hours now without success.

This simple function:
Код HTML:
stock GivePlayerMoneyEx(playerid, amount)
{
	Account[playerid][PocketMoney] += amount;
	GivePlayerMoney(playerid, Account[playerid][PocketMoney]);
	return true;
}
That function will double the amount of money without any reason. If I do GivePlayerMoney nothing happens. What is wrong?
Reply
#2

Try this one

Код:
GivePlayerMoneyEx(playerid, amount)
{
	GivePlayerMoney(playerid, (playerData[playerid][playerMoney] += amount));
	return 1;
}
Please correct if this is not what you need
Reply
#3

pawn Код:
stock Hook_GivePlayerMoney(playerid, amount)
{
    Account[playerid][PocketMoney] += amount;

    ResetPlayerMoney(playerid);
    return GivePlayerMoney(playerid, Account[playerid][PocketMoney]);
}

#if defined _ALS_GivePlayerMoney
    #undef GivePlayerMoney
#else
    #define _ALS_GivePlayerMoney
#endif

#define GivePlayerMoney Hook_GivePlayerMoney

stock SetPlayerMoney(playerid, amount)
{
    Account[playerid][PocketMoney] = amount;

    ResetPlayerMoney(playerid);
    return GivePlayerMoney(playerid, amount);
}
There's no need for any "Ex" functions. Put the hook in your script before it is used anywhere.

Use "SetPlayerMoney" when a player logs in and use "GivePlayerMoney" everywhere else.

P.S. There are 3 LSL CNR servers now? Seriously?

And so much CNR edits this year...
Reply
#4

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
pawn Код:
stock Hook_GivePlayerMoney(playerid, amount)
{
    Account[playerid][PocketMoney] += amount;

    ResetPlayerMoney(playerid);
    return GivePlayerMoney(playerid, Account[playerid][PocketMoney]);
}

#if defined _ALS_GivePlayerMoney
    #undef GivePlayerMoney
#else
    #define _ALS_GivePlayerMoney
#endif

#define GivePlayerMoney Hook_GivePlayerMoney

stock SetPlayerMoney(playerid, amount)
{
    Account[playerid][PocketMoney] = amount;

    ResetPlayerMoney(playerid);
    return GivePlayerMoney(playerid, amount);
}
There's no need for any "Ex" functions. Put the hook in your script before it is used anywhere.

Use "SetPlayerMoney" when a player logs in and use "GivePlayerMoney" everywhere else.

P.S. There are 3 LSL CNR servers now? Seriously?

And so much CNR edits this year...
Thanks.. anyways yeah i've seen it. That's why i'm starting from the bottom and doing everything properly
Reply
#5

PHP код:
stock GivePlayerMoneyEx(playeridamount)
{
    
Account[playerid][PocketMoney] += amount;
        
ResetPlayerMoney(playerid); // You just missed this
    
GivePlayerMoney(playeridAccount[playerid][PocketMoney]);
    return 
true;

You only needed to add ResetPlayerMoney first.

If you had 5000, and add 1000 to it, your server-var "PocketMoney" will hold 6000.
But since your client already has 5000, you'll be adding the 6000 to it, and you'll end up with 11000 while the server only holds 6000.

You need to reset the client money to 0 first, then send the new amount.
Reply
#6

Quote:
Originally Posted by AmigaBlizzard
Посмотреть сообщение
PHP код:
stock GivePlayerMoneyEx(playeridamount)
{
    
Account[playerid][PocketMoney] += amount;
        
ResetPlayerMoney(playerid); // You just missed this
    
GivePlayerMoney(playeridAccount[playerid][PocketMoney]);
    return 
true;

You only needed to add ResetPlayerMoney first.

If you had 5000, and add 1000 to it, your server-var "PocketMoney" will hold 6000.
But since your client already has 5000, you'll be adding the 6000 to it, and you'll end up with 11000 while the server only holds 6000.

You need to reset the client money to 0 first, then send the new amount.
No need to repeat what was already said.
Reply
#7

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
pawn Код:
stock Hook_GivePlayerMoney(playerid, amount)
{
    Account[playerid][PocketMoney] += amount;

    ResetPlayerMoney(playerid);
    return GivePlayerMoney(playerid, Account[playerid][PocketMoney]);
}

#if defined _ALS_GivePlayerMoney
    #undef GivePlayerMoney
#else
    #define _ALS_GivePlayerMoney
#endif

#define GivePlayerMoney Hook_GivePlayerMoney

stock SetPlayerMoney(playerid, amount)
{
    Account[playerid][PocketMoney] = amount;

    ResetPlayerMoney(playerid);
    return GivePlayerMoney(playerid, amount);
}
There's no need for any "Ex" functions. Put the hook in your script before it is used anywhere.

Use "SetPlayerMoney" when a player logs in and use "GivePlayerMoney" everywhere else.

P.S. There are 3 LSL CNR servers now? Seriously?

And so much CNR edits this year...
Quote:
Originally Posted by AmigaBlizzard
Посмотреть сообщение
PHP код:
stock GivePlayerMoneyEx(playeridamount)
{
    
Account[playerid][PocketMoney] += amount;
        
ResetPlayerMoney(playerid); // You just missed this
    
GivePlayerMoney(playeridAccount[playerid][PocketMoney]);
    return 
true;

You only needed to add ResetPlayerMoney first.

If you had 5000, and add 1000 to it, your server-var "PocketMoney" will hold 6000.
But since your client already has 5000, you'll be adding the 6000 to it, and you'll end up with 11000 while the server only holds 6000.

You need to reset the client money to 0 first, then send the new amount.
May I ask why you guys both decide to go for an extra function while that's not needed at all? It might not make a huge difference in performance in this case but it will when you for example do it in a really fast timer or what ever, you should always try to optimize your code as good as you can.

In this case he shouldn't use 'ResetPlayerMoney(playerid)' but simply 'GivePlayerMoney(playerid, amount)', his code was optimized and perfectly fine.

Best regards,
Jesse
Reply
#8

Quote:
Originally Posted by jessejanssen
Посмотреть сообщение
May I ask why you guys both decide to go for an extra function while that's not needed at all? It might not make a huge difference in performance in this case but it will when you for example do it in a really fast timer or what ever, you should always try to optimize your code as good as you can.

In this case he shouldn't use 'ResetPlayerMoney(playerid)' but simply 'GivePlayerMoney(playerid, amount)', his code was optimized and perfectly fine.

Best regards,
Jesse
When will you use these functions repeatedly? Never.

I find using "ResetPlayerMoney(...)" most suitable as the players' actual amount of money is stored in a variable and when you use a vending machine, a pay n' spray, a transfender, etc. your money on your hud will differ from your actual cash amount. Your money will be "corrected" (displayed correctly on your hud) when one of these functions is executed for you again.

"You should always try to optimize your code as good as you can."
You should always try to think about the possible flaws your code could cause as best as you can.
Reply
#9

I always prefer a timer that runs every second which resets a player's money and updates his money on the client using GivePlayerMoney.
When a player would hack his client-side money, it will be reset the next second, rendering his hack useless.
Unless he would be able to freeze the value displayed in his client, but that would be his problem as the script should only take server-sided amount into account when buying stuff or whatever.

That way, you only need to add the amount to the player's account and the timer will take care of the rest (updating the player's client every second with the value stored on the server).
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)