Money Textdraw, how to increase /decrease
#1

Code:
PHP код:
GivePlayerMoneyEx(playeridvalue)
{
    new 
string[20], DB_Query[256];
    
    if(
PlayerInfo[playerid][pCash] < 10)
    {
        
format(stringsizeof(string), "C000000%i"PlayerInfo[playerid][pCash]);
        
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
        
PlayerInfo[playerid][pCash] = value;
        
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"
        
PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
        
mysql_tquery(DatabaseDB_Query);
    }
    else if(
PlayerInfo[playerid][pCash] > && PlayerInfo[playerid][pCash] < 100)
    {
        
format(stringsizeof(string), "C00000%i"PlayerInfo[playerid][pCash]);
        
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
        
PlayerInfo[playerid][pCash] = value;
        
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"
        
PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
        
mysql_tquery(DatabaseDB_Query);
    }
     else if(
PlayerInfo[playerid][pCash] > 99 && PlayerInfo[playerid][pCash] < 1000)
    {
        
format(stringsizeof(string), "C0000%i"PlayerInfo[playerid][pCash]);
        
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
        
PlayerInfo[playerid][pCash] = value;
        
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"
        
PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
        
mysql_tquery(DatabaseDB_Query);
    }
    else if(
PlayerInfo[playerid][pCash] > 999 && PlayerInfo[playerid][pCash] < 10000)
    {
        
format(stringsizeof(string), "COINS 000%i"PlayerInfo[playerid][pCash]);
        
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
        
PlayerInfo[playerid][pCash] = value;
        
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"
        
PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
        
mysql_tquery(DatabaseDB_Query);
    }
    else if(
PlayerInfo[playerid][pCash] > 9999 && PlayerInfo[playerid][pCash] < 100000)
    {
        
format(stringsizeof(string), "C00%i"PlayerInfo[playerid][pCash]);
        
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
        
PlayerInfo[playerid][pCash] = value;
        
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"
        
PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
        
mysql_tquery(DatabaseDB_Query);
    }
    else if(
PlayerInfo[playerid][pCash] > 99999 && PlayerInfo[playerid][pCash] < 1000000)
    {
        
format(stringsizeof(string), "C0%i"PlayerInfo[playerid][pCash]);
        
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
        
PlayerInfo[playerid][pCash] = value;
        
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"
        
PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
        
mysql_tquery(DatabaseDB_Query);
    }
    else if(
PlayerInfo[playerid][pCash] > 999999 && PlayerInfo[playerid][pCash] < 10000000)
    {
        
format(stringsizeof(string), "C%i"PlayerInfo[playerid][pCash]);
        
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
        
PlayerInfo[playerid][pCash] = value;
        
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"
        
PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
        
mysql_tquery(DatabaseDB_Query);
    }
    return 
1;

Right now it sets the money of the player to the value that is set.
Like if a player will pay someone 12 dollars the money of that person will be set to 12 dollars.
How can I set that if he receives 12 dollars then it will be added to his value and if I use in script -value example -12 it will take 12 from him?

I tried
PlayerInfo[playerid][pCash] += value;
but it keep adding, like if the player connect and his money load it will be like this, money that loaded from database + money that loaded from the database..


So the question is.. how to detect if the value is - or +
Reply
#2

PHP код:
PlayerInfo[playerid][pCash] = GetPlayerMoney(playerid)+value
?

I don't understand what you want yet.

EDIT;

If you want to check if the value is negative, Simply;

PHP код:
if(value 0// Negative
else // Positive 
Reply
#3

Quote:
Originally Posted by oMa37
Посмотреть сообщение
PHP код:
PlayerInfo[playerid][pCash] = GetPlayerMoney(playerid)+value
?

I don't understand what you want yet.

EDIT;

If you want to check if the value is negative, Simply;

PHP код:
if(value 0// Negative
else // Positive 
Alright so, this is the function.
PHP код:
GivePlayerMoneyEx(playeridvalue
Let's say I want to take 100 from the player for a ticket, I will use this normally.

PHP код:
GivePlayerMoneyEx(playerid, -100
But when the function will get called it will be like this
PlayerInfo[playerid][pCash] = -100?
Because it is PlayerInfo[playerid][pCash] = value; and if the value is -100, will it work and take money from the player?

Also If I want to give the player 100 dollars I will use this
PHP код:
GivePlayerMoneyEx(playerid100
And the function will get called like this
PlayerInfo[playerid][pCash] = value; which value will be 100, and pCash is = with value, that means it will set whole his money to 100.

I tried using PlayerInfo[playeri][pCash] += value, but then any value will be added to his money which is not good.
Example if I use
GivePlayerMoneyEx(playerid, PlayerInfo[playerid][pCash]) to load his money it will double his money.


I want if the player receives money it will add to his current money and if something takes money from him it will decrease his money.


Example:
Player has 2000 COINS, thats how they are called on my server.

if he buys a soda I will use this (soda price 20 COINS)
GivePlayerMoneyEx(playerid, -20) ;
The function will call:
PlayerInfo[playerid][pCash] = value;
value = -20
PlayerInfo[playerid][pCash] = -20; ??

And lets say the player wins an event and I want to give him 100 coins as prize
GivePlayerMoneyEx(playerid, 100) ;
Reply
#4

PlayerInfo[playerid][pCash] += value
Reply
#5

Quote:
Originally Posted by Spmn
Посмотреть сообщение
PlayerInfo[playerid][pCash] += value
Did you even read what I wrote?

I said that if I use that it will add to the cash any value
example

If I load the money

GivePlayerMoneyEx(playerid, PlayerInfo[playerid][pCash]);
it will double them because
PlayerInfo[playerid][pCash] += value
value = PlayerInfo[playerid][pCash]
PlayerInfo[playerid][pCash] + PlayerInfo[playerid][pCash]



....



Anyway, I solved it, I made 3 functions
GivePlayerMoney
TakePlayerMoney
SetPlayerMoney
Reply
#6

ResetPlayerMoney();
GivePlayerMoney();

Keep it simple.
Reply
#7

Quote:
Originally Posted by Spmn
Посмотреть сообщение
ResetPlayerMoney();
GivePlayerMoney();

Keep it simple.
I don't want to use the default currency thats why I did this, why should I keep it simple and use that if I want to have my own currency...
Reply
#8

What I wanted to say is that if you want to SET player money, you firstly have to reset it and then give money.
Eg:
Код:
// Player 1 has $0
GivePlayerCash(1, 500);
// now player 1 has $500
ResetPlayerCash(1);
GivePlayerCash(1, 10000);
// now player 1 money was set to $1000
Reply
#9

Or just use SetPlayerMoney.
Reply
#10

PHP код:
GivePlayerMoneyEx(playeridvalue)
{
    new 
string[20], DB_Query[70];
    
PlayerInfo[playerid][pCash] = PlayerInfo[playerid][pCash] + value;
    
format(stringsizeof(string), "C%07i"PlayerInfo[playerid][pCash]);
    
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string);
    
mysql_format(DatabaseDB_Querysizeof(DB_Query), "UPDATE `USERS` SET `CASH` = %d WHERE `ID` = %d"PlayerInfo[playerid][pCash], PlayerInfo[playerid][ID]);
    
mysql_tquery(DatabaseDB_Query);
    return 
1;

Much shorter and does exactly the same thing.
Your query variable was too large as well, 70 characters is more than enough, no need for 256 characters.

The "%07i" makes sure the value is always displayed with 7 digits.

And upon loading the money from the database, don't use this function but just set it like this in your loading function:
PHP код:
    cache_get_value_name_int(0"CASH"PlayerInfo[playerid][pCash]);
    
format(stringsizeof(string), "C%07i"PlayerInfo[playerid][pCash]);
    
PlayerTextDrawSetString(playeridMoneyText[0][playerid], string); 
Then it won't overwrite anything, it won't update anything, you won't get buggy stuff like always doubling your money when you log in.

Loading is loading and is done only once, you don't need to use a function that will update it as well.


And when you want to take money away from the player, use code like this to do it:
PHP код:
GivePlayerMoneyEx(playerid, -100);
or
GivePlayerMoneyEx(playerid, -variable); 
The - sign makes the value negative and the function will automatically substract the given value from the player's money.
No need to make a GiveMoney and TakeMoney function.

And if you want to reset a player's money, you can do this:
PHP код:
GivePlayerMoneyEx(playerid, -PlayerInfo[playerid][pCash]); 
It will then get the player's money from "PlayerInfo[playerid][pCash]", make it negative and add that to the player's money.
In the end, it will do this:
PlayerInfo[playerid][pCash] = PlayerInfo[playerid][pCash] + (-PlayerInfo[playerid][pCash]);
As you know, + and - behind eachother becomes -.

So it will do this:
PlayerInfo[playerid][pCash] = PlayerInfo[playerid][pCash] - PlayerInfo[playerid][pCash];

Or (if the money would be 5187 for example):
PlayerInfo[playerid][pCash] = 5187 - 5187;

Result = 0.

Simple math.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)