trouble with Biz saving for player
#1

hey guys i got this problem that my business system dosent save for player if they buy it after relog there wont be no business for player but shows that he owns it. (( i know you cant do it without a code to help me but just tell me please what i need to have to save biz for player ))

i just looked it though there is everything in the place also this

pawn Код:
stock SaveBiz()
{
    if(!fexist("biz.cfg")) fcreate("biz.cfg");
    new idx = 1, File:file;
    new string[256];
    while(idx < MAX_BIZ)
    {
        format(string, sizeof(string), "%d|%d|%s|%f|%f|%f|%d|%d|%d|%d|%d|%d\r\n", BizInfo[idx][bType], BizInfo[idx][bStatus], BizInfo[idx][bOwner], BizInfo[idx][bX], BizInfo[idx][bY], BizInfo[idx][bZ], BizInfo[idx][bMoney], BizInfo[idx][bProducts], BizInfo[idx][bSold], BizInfo[idx][bLevel], BizInfo[idx][bPrice], BizInfo[idx][bAP]);
        if(idx == 1)
        {
            file = fopen("biz.cfg", io_write);
        }
        else
        {
            file = fopen("biz.cfg", io_append);
        }
        fwrite(file, string);
        fclose(file);
        idx++;
    }
    print("Businesses saved at script file biz.cfg successfully.");
}
Thnaks for the help!
Reply
#2

pawn Код:
stock SaveBiz()
{
    if(!fexist("biz.cfg")) fcreate("biz.cfg");
    new idx = 1, File:file;
    new string[256];
    while(idx < MAX_BIZ)
    {
        format(string, sizeof(string), "%d|%d|%s|%f|%f|%f|%d|%d|%d|%d|%d|%d\r\n", BizInfo[idx][bType], BizInfo[idx][bStatus], BizInfo[idx][bOwner], BizInfo[idx][bX], BizInfo[idx][bY], BizInfo[idx][bZ], BizInfo[idx][bMoney], BizInfo[idx][bProducts], BizInfo[idx][bSold], BizInfo[idx][bLevel], BizInfo[idx][bPrice], BizInfo[idx][bAP]);
        if(idx == 1)
        {
            file = fopen("scriptfiles/business/biz.cfg", io_write);
        }
        else
        {
            file = fopen("scriptfiles/business/biz.cfg", io_append);
        }
        fwrite(file, string);
        fclose(file);
        idx++;
    }
    print("Businesses saved at script file biz.cfg successfully.");
}
Try and tell result
Reply
#3

weird... now when i do /buybiz it gives me the biz then crashes and then rollback (( no biz ))
Reply
#4

pawn Код:
stock SaveBiz()
{
 
    new idx = 1, File:file;
    new string[256];
    while(idx < MAX_BIZ)
    {
        format(string, sizeof(string), "%d|%d|%s|%f|%f|%f|%d|%d|%d|%d|%d|%d\r\n", BizInfo[idx][bType], BizInfo[idx][bStatus], BizInfo[idx][bOwner], BizInfo[idx][bX], BizInfo[idx][bY], BizInfo[idx][bZ], BizInfo[idx][bMoney], BizInfo[idx][bProducts], BizInfo[idx][bSold], BizInfo[idx][bLevel], BizInfo[idx][bPrice], BizInfo[idx][bAP]);
       
        file = fopen("biz.cfg", io_append);
        fwrite(file, string);
        fclose(file);
        idx++;
    }
    print("Businesses saved at script file biz.cfg successfully.");
}
io_write always clears the data from the file and then write so dont use it
no need for creating file too caz if file doesnt exit io_append automatically creates it
try my code
Reply
#5

- No need to open and close a file many times so don't use it inside the loop. Do it once out of the loop.
- You never check if the file handle is valid before writing to/closing the file and that can crash the server.
- Indexes from arrays always start from 0 and not 1 so it goes waste.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)