Error gets spammed
#1

Hello,
pawn Код:
CMD:buybizz(playerid, params[])
{
if(pInfo[playerid][OwnedBizz] == 1) return SendClientMessage(playerid,COLOR_PINK2,"Error: You already own a business");
for(new i; i < MAX_BUSINESSES; i++)
{
if(IsPlayerInRangeOfPoint(playerid, 10.0, bInfo[i][Position][0], bInfo[i][Position][1], bInfo[i][Position][2]))
{
if(pInfo[playerid][pMoney] >= bInfo[i][Price])
{
if(strcmp(bInfo[i][bOwner], "Nobody", true) == 0)
{
new query[500],query2[500];
format(bInfo[i][bOwner], MAX_PLAYER_NAME, "%s", GetName(playerid));
pInfo[playerid][pMoney] -= bInfo[i][Price];
pInfo[playerid][pBizz] = bInfo[i][ID];
pInfo[playerid][OwnedBizz] = 1;
format(query,sizeof(query),"UPDATE businesses SET `owner` = '%s' WHERE `id` =%d",bInfo[i][bOwner],bInfo[i][ID]);
mysql_query(query);
format(query2,sizeof(query2),"UPDATE `accounts` SET `BusinessID` =%i,`OwnedBizz` = %i WHERE `id` =%d LIMIT 1", pInfo[playerid][pBizz],pInfo[playerid][OwnedBizz], pInfo[playerid][ID]);
mysql_query(query2);
print(query2);
print(query);
new string2[500];
format(string2, sizeof(string2), "%s\nOwner: %s\nPrice: %i\n ID: %i\nEntry Fee: %i",bInfo[i][bName],bInfo[i][bOwner], bInfo[i][Price],bInfo[i][ID],bInfo[i][bEntryFee]);
Update3DTextLabelText(bInfo[i][LabelID], 0xB0D5E8FF, string2);
SendClientMessage(playerid,0x9EC7DEFF,"Congratulations!, You have successfully bought the business");
}
else
{
    SendClientMessage(playerid,COLOR_PINK2,"Error: This business is already owned");
}
}
else
{
    SendClientMessage(playerid,COLOR_PINK2,"Error: You do not have enough cash");
}
}
else
{
    SendClientMessage(playerid,COLOR_PINK2,"Error: You are not near any property");
}
}
return 1;
}
Ok so, whenever i go to buy a bizz and do /buybizz i can buy it but it spams the chat with with "Error: You are not near any property" but i can still buy the bizz. I tried putting multiple coding in but meh nothing worked.

I hope someone can resolve this problem.
Reply
#2

You're sending the message inside a loop. It will send the message X times the amount of business the player is not in range of.

This can be solved by doing all the code processing Outside the loop by this way:
The following code gets the ID of the business the player is in range of and will store it in a variable ('i')

pawn Код:
new i = 0;
for(new a; a < MAX_BUSINESSES; a++)
{
       if(!IsPlayerInRangeOfPoint(playerid, 10.0, bInfo[a][Position][0], bInfo[a][Position][1], bInfo[a][Position][2]))
             continue;

       i= a;
}

if(!i)
    return SendClientMessage(playerid, COLOR_PINK2, "Error: you are not near any property");
You're also eating a lot of resources by creating a 500 cell string inside the loop. I am sure 500 cells are not needed for such a little message.

P.S: I defined as 'i' so you don't have to change most of the code.
Reply
#3

Hmm what you said seems to work but the problem is i can now buy properties even if i dont have enough cash, and the property i am trying to buy is ID 1 but when i go to ID 2 and the price of it is $1 and inhand i have around 1k it says "Error: You do not have enough cash"
Reply
#4

pawn Код:
CMD:buybizz(playerid, params[])
{
    if(pInfo[playerid][OwnedBizz] == 1)
        return SendClientMessage(playerid,COLOR_PINK2,"Error: You already own a business");

    new bizzid = 0;
    for(new a; a < MAX_BUSINESSES; a++)
    {
           if(!IsPlayerInRangeOfPoint(playerid, 10.0, bInfo[a][Position][0], bInfo[a][Position][1], bInfo[a][Position][2]))
                 continue;

           bizzid = a;
    }

    if(!bizzid)
        return SendClientMessage(playerid, COLOR_PINK2, "Error: you are not near any property.");
       
       
    if(pInfo[playerid][pMoney] < bInfo[bizzid][Price])
        return SendClientMessage(playerid, COLOR_PINK2, "Error: you don't have money.");

    if(strcmp(bInfo[bizzid][bOwner], "Nobody", true))
        return SendClientMessage(playerid, COLOR_PINK2, "Error: this already belongs to someone"); //note that there could be a player named nobody!!

    new query[128],query2[128];
    format(bInfo[bizzid][bOwner], MAX_PLAYER_NAME, "%s", GetName(playerid));
    pInfo[playerid][pMoney] -= bInfo[bizzid][Price];
    pInfo[playerid][pBizz] = bInfo[bizzid][ID];
    pInfo[playerid][OwnedBizz] = 1;
   
    format(query,sizeof(query),"UPDATE businesses SET `owner` = '%s' WHERE `id` =%d",bInfo[bizzid][bOwner],bInfo[bizzid][ID]);
    mysql_query(query);
    format(query2,sizeof(query2),"UPDATE `accounts` SET `BusinessID` =%i,`OwnedBizz` = %i WHERE `id` =%d LIMIT 1", pInfo[playerid][pBizz],pInfo[playerid][OwnedBizz], pInfo[playerid][ID]);
    mysql_query(query2);
   
    print(query2);
    print(query);
   
    new string2[144];
    format(string2, sizeof(string2), "%s\nOwner: %s\nPrice: %i\n ID: %i\nEntry Fee: %i",bInfo[bizzid][bName],bInfo[bizzid][bOwner], bInfo[bizzid][Price],bInfo[bizzid][ID],bInfo[bizzid][bEntryFee]);
    Update3DTextLabelText(bInfo[bizzid][LabelID], 0xB0D5E8FF, string2);
   
    SendClientMessage(playerid,0x9EC7DEFF,"Congratulations!, You have successfully bought the business");
    return 1;
}
Try that; I highly believe 10.0 is too high for a range checking on this kind of things, if there are two bizzes within that range it will only take the one with the latter memory id.
Reply
#5

Still, it lets me buy even if i have -1$
Reply
#6

Quote:
Originally Posted by Domnic Toretto
Посмотреть сообщение
Still, it lets me buy even if i have -1$
Are you sure your pmoney/bizzprice variable is set correctly then? Is there any kind of variable to determine whether the bizz is not set or not? That could be added to the loop.
Reply
#7

hmm lemme just switch something around and see.

P.S Is there any kind of variable to determine whether the bizz is not set or not?

What do you mean by that?
Reply
#8

Ye you were right, i needed to get the players money , i added
pawn Код:
pInfo[playerid][pMoney] = GetPlayerMoney(playerid);
and it eventually got fixed
Reply
#9

Quote:
Originally Posted by Domnic Toretto
Посмотреть сообщение
hmm lemme just switch something around and see.

P.S Is there any kind of variable to determine whether the bizz is not set or not?

What do you mean by that?
Well, in the loop you're looping thru the max business number availble. If MAX_BUSINESS is 50 and you have only set 40 you would be wasting 10 loop iterations; Scale that to 1000 and you get 960 wasted iterations, where you would be checking 960 times if the player is in range of x0.0 y0.0 z0.0. If Businesses ID's are incremental then you could check if the ID is not 0 (starting from 1), if it is it is mean that the business is not set and then skip it.


Quote:
Originally Posted by Domnic Toretto
Посмотреть сообщение
Ye you were right, i needed to get the players money , i added
pawn Код:
pInfo[playerid][pMoney] = GetPlayerMoney(playerid);
and it eventually got fixed
There's no need for pMoney variable if you're not using a scriptside Money system. Just save into the file with GetPlayerMoney and load it with GivePlayerMoney;
Reply
#10

Quote:
Originally Posted by CuervO
Посмотреть сообщение
Well, in the loop you're looping thru the max business number availble. If MAX_BUSINESS is 50 and you have only set 40 you would be wasting 10 loop iterations; Scale that to 1000 and you get 960 wasted iterations, where you would be checking 960 times if the player is in range of x0.0 y0.0 z0.0. If Businesses ID's are incremental then you could check if the ID is not 0 (starting from 1), if it is it is mean that the business is not set and then skip it.




There's no need for pMoney variable if you're not using a scriptside Money system. Just save into the file with GetPlayerMoney and load it with GivePlayerMoney;
Followed your instruction's its fixed, P.S i use the pMoney variable for a seperate banking system so ye, anways Thanks ALOT for helping mate, +repped.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)