[Tutorial] Bank System || Y_INI and ZCMD
#1

Helloooo there, Extraordinariness here!
I will show you how to make a Bank System using Y_INI and ZCMD!

What is Bank System?
Bank system is when you create your own bank, just like in real life, where you store money so it could be safe.

OK, let's start!

I. Deposit
pawn Код:
CMD:deposit(playerid, params[])
We will first start off in our deposit script. You could put COMMAND: instead of CMD: for the first one.

pawn Код:
new money, string[128];
We are creating new variables called "money" and "string". Money, is the variable, where we could put the money. and string, as our messager, which means we will use it to send to you that it is successful.

pawn Код:
if(sscanf(params, "d", money)) return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /deposit [money]");
So the "d" means an integer/or whole number. So if we could put "/deposit shit" it would return as "USAGE: /deposit [money]" because it isn't a numerical number. The money beside "d" is the variable of what I'm typing about earlier.

When they have failed on doing /deposit "numerical value", it will return to you(playerid) with the color (0xFF0000FF - red) like this: "USAGE: /deposit [money]".

pawn Код:
if(GetPlayerMoney(playerid) < money) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You have not enough money on your pocket to put in your bank!");
We will now get the player's money. (GetPlayerMoney(playerid) < money) this means if the player's money is not enough for the variable "money", it will return as "ERROR: You have not enough money...".

pawn Код:
new money1 = money;
We will create a new variable of money. I'll explain it later.

pawn Код:
PlayerInfo[playerid][pBankmoney] += money;
You can also use your own variable for it. And, this means, the 'money', or the numerical value that you have read earlier, will add to the variable Bankmoney.

pawn Код:
format(string, sizeof(string), ""COL_GREEN"You have deposited $%d into your bank account thus your new balance is $%d.", money1, PlayerInfo[playerid][pBankmoney]);
FORMAT: so we could store variables in a text. SendClientMessage doesn't support it(strings), so format is supports variables. Now, %d means like earlier, which is a numerical value. And it said, you have deposited $%d, because we have deposited that number. The second %d is a numerical value, which will say the new balance of your bank.

We have used the string here. Look at (string, sizeof(string).

Eyes up here on the next paragraph. This is a little bit hard to understand.
The leftovers, which is money1, and PlayerInfo[playerid][pBankmoney], represents the two %s's. First %s, is our first variable, when you /withdraw 500, it will show as 500. Because, that "money" presents as the first parameter. The next one, the next %d, is added earlier, which we will add it on the variable "money", so it will add, and makes the second %s.

pawn Код:
SendClientMessage(playerid, -1, string);
Send to player, the string, if you don't get what I've said earlier, SCM doesnt SUPPORT VARS. but FORMAT does. SO, we can use SCM to send FORMAT to you. Get it? If not, read it again.

pawn Код:
GivePlayerMoney(playerid, -money);
        return 1;
We will now reduce the player's money via "money" variable, which we be reduced on whatever you type. And don't forget to add return, and the bracket.


_____________________


II. Balance
pawn Код:
CMD:balance(playerid, params[])
make new cmd of balance.

pawn Код:
new string[128];
We will create new string with the max letters of 128.

pawn Код:
format(string, sizeof(string), ""COL_GREEN"Your current bank balance is $%d.",PlayerInfo[playerid][pBankmoney]);
Use format to store variables in a text, and put another %d again for another numerical value to be put on the text. Now, we have put there the PlayerInfo[playerid][pBankmoney] again to show the whole money of your bank.

pawn Код:
SendClientMessage(playerid, -1, string);
    return 1;
As I said earlier.


_____________________


III. Withdraw
Basically this is just the same with DEPOSIT, just edit some few changes.

Replace this one.
pawn Код:
if(PlayerInfo[playerid][pBankmoney] < money) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You have not enough money on your bank to put in your pocket!");
The PAWN Compiler will see if the Player bank money has the sufficient money to withdraw. If not, SCM appears.

Replace this one.
pawn Код:
PlayerInfo[playerid][pBankmoney] -= money;
We will reduce the money. Earlier, it's +=.

Replace this one.
pawn Код:
format(string, sizeof(string), ""COL_GREEN"You have withdrawed $%d into your bank account thus your new balance is $%d.", money1, PlayerInfo[playerid][pBankmoney]);
Change the text, and still make two %d's.

Make this stay.
pawn Код:
SendClientMessage(playerid, -1, string);
    GivePlayerMoney(playerid, money);
    return 1;
}
_________________________


You have finished doing BANK SYSTEM!
Now, we have a new problem! How do we save the BANK MONEY?
For this, we will use Y_INI, by ******!

Create a new enum.(I will use pInfo/PlayerInfo for a while.)
pawn Код:
//userpaths
stock UserPath(playerid)
{
    new string[128],playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid,playername,sizeof(playername));
    format(string,sizeof(string),PATH,playername);
    return string;
}

enum pInfo
{
    pBankmoney
}
new PlayerInfo[MAX_PLAYERS][pInfo];
pawn Код:
//Everywhere on your script
forward LoadUser_data(playerid,name[],value[]);
public LoadUser_data(playerid,name[],value[])
{
INI_Int("Bank",PlayerInfo[playerid][pBankmoney]);
return 1;
}
This means we will forward this, so no error will come out on your compiler, and make a new public.

pawn Код:
//Put on OnPlayerDisconnect
{
   new INI:File = INI_Open(UserPath(playerid));
    INI_SetTag(File,"data");
    INI_WriteInt(File,"Bank",PlayerInfo[playerid][pBankmoney]);
    INI_Close(File);
    return 1;
}
pawn Код:
new INI:File = INI_Open(UserPath(playerid));
We will open a new INI File using the public of USERPATH.

pawn Код:
INI_SetTag(File,"data");
We will set the tag, which it will say on .ini file, [data].

pawn Код:
INI_WriteInt(File,"Bank",PlayerInfo[playerid][pBankmoney]);
The Y_INI will write the variable[pBankmoney] in .ini.

pawn Код:
INI_Close(File);
INI will close the file, so it will stop saving.

You can now save! But I am now busy. Sorry if I say something really short.
This is very detailed(for me), so I guess, if you still can't, re read.
Thanks - ZCMD, Y_INI, SA:MP, and https://sampforum.blast.hk/showthread.php?tid=502840.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)