SA-MP Forums Archive
Buy House Command -_- - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Buy House Command -_- (/showthread.php?tid=578175)



Buy House Command -_- - nezo2001 - 17.06.2015

When the command run it changed all the data in the database not only the data of the certain house which the player should buy
PHP код:
CMD:buyhouse(playeridparams[])
{
    for(new 
a!= HouseLoad; ++a)
    {
        if(
IsPlayerInRangeOfPoint(playerid3.0HouseInfo[a][EntranceX], HouseInfo[a][EntranceY], HouseInfo[a][EntranceZ]))
        {
            if(
HouseInfo[a][HousePrice] > GetPlayerMoney(playerid)) return SendClientMessage(playeridCOLOR_RED"You don't have enough money!");
            new 
draw[200];
            
format(draw,sizeof(draw),"ID: %i\nPrice %i\nOwned: Yes\nOwner: %s",PlayerName(playerid));
            new 
query[200];
            
format(querysizeof(query), "UPDATE houses SET owner='%s', owned='true', text='%s' WHERE id='%i'"PlayerName(playerid), drawHouseInfo[a][HouseID]);
            
mysql_query(1query);
            
Update3DTextLabelText(HouseLabelCOLOR_GREENHouseInfo[a][HouseText]);
            
GivePlayerMoney(playerid, -HouseInfo[a][HousePrice]);
            
PlayerInfo[playerid][pHouseID] = HouseInfo[a][HouseID];
            
SendClientMessage(playeridCOLOR_GREEN"You've bought this house successfully!");
        }
        else
        {
            
SendClientMessage(playeridCOLOR_RED"You are not near any house");
        }
    }
    return 
1;

And it take the money from the player and also send a message that he isn't near the house?!


Re: Buy House Command -_- - Darrenr - 17.06.2015

--removed--


Re: Buy House Command -_- - Darrenr - 17.06.2015

You need to "break;" the loop also.

At the end of the if statement, add a "break;" to tell the code to stop looping through the code (as you have now bought a house)

https://sampwiki.blast.hk/wiki/Control_Structures#break


Re: Buy House Command -_- - nezo2001 - 17.06.2015

So what is the condition to break it


Re: Buy House Command -_- - Darrenr - 17.06.2015

Quote:
Originally Posted by nezo2001
Посмотреть сообщение
So what is the condition to break it
Read the wiki page that i posted above.

The idea of BREAK is to terminate the for loop which you are running to find what the id is of the house the player is standing at. Once the house is found in the loop, you can terminate the loop. The code would look like this below:

Код:
CMD:buyhouse(playerid, params[])
{
    for(new a; a != HouseLoad; ++a)
    {
        if(IsPlayerInRangeOfPoint(playerid, 3.0, HouseInfo[a][EntranceX], HouseInfo[a][EntranceY], HouseInfo[a][EntranceZ]))
        {
            if(HouseInfo[a][HousePrice] > GetPlayerMoney(playerid)) return SendClientMessage(playerid, COLOR_RED, "You don't have enough money!");
            new draw[200];
            format(draw,sizeof(draw),"ID: %i\nPrice %i\nOwned: Yes\nOwner: %s",PlayerName(playerid));
            new query[200];
            format(query, sizeof(query), "UPDATE houses SET owner='%s', owned='true', text='%s' WHERE id='%i'", PlayerName(playerid), draw, HouseInfo[a][HouseID]);
            mysql_query(1, query);
            Update3DTextLabelText(HouseLabel, COLOR_GREEN, HouseInfo[a][HouseText]);
            GivePlayerMoney(playerid, -HouseInfo[a][HousePrice]);
            PlayerInfo[playerid][pHouseID] = HouseInfo[a][HouseID];
            SendClientMessage(playerid, COLOR_GREEN, "You've bought this house successfully!");
            break; // terminates the loop as we are finished searching for a house to buy
        }
        else
        {
            SendClientMessage(playerid, COLOR_RED, "You are not near any house");
        }
    }
    return 1;
}



Re: Buy House Command -_- - nezo2001 - 17.06.2015

Ok I did it but it send me that I bought this house and I'm not near any house at the same time


Re: Buy House Command -_- - Konstantinos - 17.06.2015

Don't send the message about not in range in the loop then!

PHP код:
CMD:buyhouse(playeridparams[])
{
    new 
boolin_range;
    for(new 
a!= HouseLoad; ++a)
    {
        if(
IsPlayerInRangeOfPoint(playerid3.0HouseInfo[a][EntranceX], HouseInfo[a][EntranceY], HouseInfo[a][EntranceZ]))
        {
            if(
HouseInfo[a][HousePrice] > GetPlayerMoney(playerid)) return SendClientMessage(playeridCOLOR_RED"You don't have enough money!");
            new 
draw[200];
            
format(draw,sizeof(draw),"ID: %i\nPrice %i\nOwned: Yes\nOwner: %s",PlayerName(playerid));
            new 
query[200];
            
format(querysizeof(query), "UPDATE houses SET owner='%s', owned='true', text='%s' WHERE id='%i'"PlayerName(playerid), drawHouseInfo[a][HouseID]);
            
mysql_query(1query);
            
Update3DTextLabelText(HouseLabelCOLOR_GREENHouseInfo[a][HouseText]);
            
GivePlayerMoney(playerid, -HouseInfo[a][HousePrice]);
            
PlayerInfo[playerid][pHouseID] = HouseInfo[a][HouseID];
            
SendClientMessage(playeridCOLOR_GREEN"You've bought this house successfully!");
            
in_range true;
            break;
        }
    }
    if (!
in_rangeSendClientMessage(playeridCOLOR_RED"You are not near any house");
    return 
1;

By the way, I don't know about the update thing you mentioned but the query seems correct and there is a WHERE clause. Print the query and execute it through phpMyAdmin, does it update only for that specific house?


Re: Buy House Command -_- - Darrenr - 17.06.2015

--remove--


Re: Buy House Command -_- - nezo2001 - 17.06.2015

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Don't send the message about not in range in the loop then!

PHP код:
CMD:buyhouse(playeridparams[])
{
    new 
boolin_range;
    for(new 
a!= HouseLoad; ++a)
    {
        if(
IsPlayerInRangeOfPoint(playerid3.0HouseInfo[a][EntranceX], HouseInfo[a][EntranceY], HouseInfo[a][EntranceZ]))
        {
            if(
HouseInfo[a][HousePrice] > GetPlayerMoney(playerid)) return SendClientMessage(playeridCOLOR_RED"You don't have enough money!");
            new 
draw[200];
            
format(draw,sizeof(draw),"ID: %i\nPrice %i\nOwned: Yes\nOwner: %s",PlayerName(playerid));
            new 
query[200];
            
format(querysizeof(query), "UPDATE houses SET owner='%s', owned='true', text='%s' WHERE id='%i'"PlayerName(playerid), drawHouseInfo[a][HouseID]);
            
mysql_query(1query);
            
Update3DTextLabelText(HouseLabelCOLOR_GREENHouseInfo[a][HouseText]);
            
GivePlayerMoney(playerid, -HouseInfo[a][HousePrice]);
            
PlayerInfo[playerid][pHouseID] = HouseInfo[a][HouseID];
            
SendClientMessage(playeridCOLOR_GREEN"You've bought this house successfully!");
            
in_range true;
            break;
        }
    }
    if (!
in_rangeSendClientMessage(playeridCOLOR_RED"You are not near any house");
    return 
1;

By the way, I don't know about the update thing you mentioned but the query seems correct and there is a WHERE clause. Print the query and execute it through phpMyAdmin, does it update only for that specific house?
Yes it did, And I will re-load it again to update the text, right?