SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 21/03/2020, 09:23 AM   #1
ImTobi
Big Clucker
 
ImTobi's Avatar
 
Join Date: Jun 2017
Posts: 144
Reputation: 3
Default Buyhouse command not working

Code:
CMD:buyhouse(playerid,params[])
{
	if(!IsPlayerLoggedIn(playerid))return notloggedin(playerid);
	new file[128];
	new Float:x,Float:y,Float:z;
	new string[128];
	GetPlayerPos(playerid, x, y, z);
	for(new i=0; i<sizeof(hInfo); i++)
	{
		format(file,sizeof(file),"/houses/%i.ini",i);
		if(fexist(file))
		{
			if(IsPlayerInRangeOfPoint(playerid, 3, hInfo[i][henterx], hInfo[i][hentery], hInfo[i][henterz]))
			{
					if(!strlen(hInfo[i][hBesitzer]))
					{
						if(GetPlayerMoney(playerid) < hInfo[i][hPrice])return SendClientMessage(playerid, COLOR_RED, "Du hast nicht genügend Geld.");
						GivePlayerMoney(playerid, -hInfo[i][hPrice]);
						strcpy(hInfo[i][hBesitzer],getPlayerName(playerid),128);
						format(string,sizeof(string),"Besitzer: %s",getPlayerName(playerid));
						UpdateDynamic3DTextLabelText(hInfo[i][hBesitzerText], COLOR_WHITE, string);
						DestroyPickup(hInfo[i][hPickup]);
						hInfo[i][hPickup] = CreatePickup(1272, 1, hInfo[i][henterx], hInfo[i][hentery], hInfo[i][henterz], -1);
						dini_Set(file,"Besitzer",getPlayerName(playerid));
					}
					return SendClientMessage(playerid, COLOR_RED, "Das Haus hat bereits einen Besitzer.");
			}
			return SendClientMessage(playerid, COLOR_RED, "Du bist nicht in der Nähe eines Hauses.");
		}
		return 1;
	}
	return 1;
}

CMD:createhouse(playerid,params[])
{
	if(!IsPlayerLoggedIn(playerid))return notloggedin(playerid);
	if(!IsAdmin(playerid,1))return noadmin(playerid);
	new InteriorID,ID,Float:InteriorX,Float:InteriorY,Float:InteriorZ,HousePrice;
	if(sscanf(params,"iifffi",ID,InteriorID,InteriorX,InteriorY,InteriorZ,HousePrice))return SendClientMessage(playerid, COLOR_RED, "INFO: /createtesthouse [ID][Interior][X][Y][Z][Verkaufspreis]");
	new file[128];
	format(file,sizeof(file),"/houses/%i.ini",ID);
	if(fexist(file))return SendClientMessage(playerid, COLOR_RED,"INFO: Die ID ist schon vergeben, benutze eine andere.");
	dini_Create(file);
	dini_IntSet(file,"ID",ID);
	dini_FloatSet(file,"InteriorX",InteriorX);
	dini_FloatSet(file,"InteriorY",InteriorY);
	dini_FloatSet(file,"InteriorZ",InteriorZ);
	dini_IntSet(file,"Interior",InteriorID);
	dini_IntSet(file,"HousePrice",HousePrice);
	new Float:x,Float:y,Float:z;
	GetPlayerPos(playerid,x,y,z);
	dini_FloatSet(file,"X",x);
	dini_FloatSet(file,"Y",y);
	dini_FloatSet(file,"Z",z);
	dini_IntSet(file,"Locked",1);
	hInfo[ID][hPickup] = CreatePickup(1273, 1, x, y, z,-1);
	new string[128];
	format(string,sizeof(string),"Besitzer: Niemand\n~g~Zum Verkauf~w~\nPreis: %i",HousePrice);
	hInfo[ID][hBesitzerText] = CreateDynamic3DTextLabel(string, COLOR_WHITE, x,y, z,20, INVALID_PLAYER_ID,INVALID_VEHICLE_ID, 1);
	hInfo[ID][hPrice] = HousePrice;
	hInfo[ID][henterx] = x;
	hInfo[ID][hentery] = y;
	hInfo[ID][henterz] = z;
	hInfo[ID][hexitx] = InteriorX;
	hInfo[ID][hexity] = InteriorY;
	hInfo[ID][hexitz] = InteriorZ;
	hInfo[ID][hInterior] = InteriorID;
	hInfo[ID][hLocked] = 1;
	strcpy(hInfo[ID][hBesitzer],"",128);
	return 1;
}

it says i'm not near any houses, after i create a house and stand in the pickup

and please no dumb comments about dini,i have dini v2 and i like it
ImTobi is offline   Reply With Quote
Old 21/03/2020, 09:45 AM   #2
SiaReyes
Gangsta
 
SiaReyes's Avatar
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 622
Reputation: 57
Default Re: Buyhouse command not working

Try 'else return SendClientMessage(...);'
SiaReyes is offline   Reply With Quote
Old 21/03/2020, 10:20 AM   #3
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Buyhouse command not working

Never return in a loop unless in range.

What happens in your code:
- You are near houseid 3.
- Loop starts.
- Houseid 0, not in range. Sends message you are not near any houses and stops.

Do not check if every file house exists, use a variable to check if a house exists or not.
__________________
Calisthenics is offline   Reply With Quote
Old 21/03/2020, 11:55 AM   #4
ImTobi
Big Clucker
 
ImTobi's Avatar
 
Join Date: Jun 2017
Posts: 144
Reputation: 3
Default Re: Buyhouse command not working

Can you give me an eample how the code should looklike? because i did it like i did before in a command, and that worked also with return after the loop, with every car that i wanted to check
ImTobi is offline   Reply With Quote
Old 21/03/2020, 12:30 PM   #5
Bwandon
Little Clucker
 
Bwandon's Avatar
 
Join Date: Jan 2017
Location: Great Britiain
Posts: 28
Reputation: 11
Default Re: Buyhouse command not working

Try this? I also created a function (GetoutHouse) that when called checks if your ourside a house + return the houseid - that will most likely come in use for further commands such as /enter, /sellhouse and other management.

The reason your code wasn't working is because you was returning errors inside the loop, for example:
Loop starts -> checks if player is outside house 0 if not return error. -> end loop at first interval (return sendclien...)

for future reference you can use 'break' to stop a loop or 'continue' to skip to next interval. This is the reason i created the function as an example for you. Goodluck.

pawn Code:
stock GetoutHouse(playerid)
{
    new houseid = -1,
        Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
    new houseloc[45];
    for(new house, i = sizeof(hInfo); house != i; house++)
    {
        format(houseloc, sizeof(houseloc), "/houses/%i.ini", house);
        if(!fexist) continue;
        if(IsPlayerInRangeOfPoint(playerid, 3, hInfo[house][henterx], hInfo[house][hentery], hInfo[house][henterz]))
        {
            houseid = house;
            break;
        }
    }    
    return houseid;
}

CMD:buyhouse(playerid, params[])
{
    if(!IsPlayerLoggedIn(playerid)) return notloggedin(playerid);
    new houseid = GetoutHouse(playerid);
    if(houseid != -1)
    {
        if(!strlen(hInfo[i][hBesitzer]))
        {
            new houseloc[45]; format(houseloc, sizeof(houseloc), "/houses/%i.ini", houseid);
            if(GetPlayerMoney(playerid) < hInfo[houseid][hPrice])
            {
                SendClientMessage(playerid, COLOR_RED, "Du hast nicht genügend Geld.");
                return 1;
            }
            GivePlayerMoney(playerid, -hInfo[houseid][hPrice]);
            strcpy(hInfo[houseid][hBesitzer], getPlayerName(playerid),128);
            new string[MAX_PLAYER_NAME*2];
            format(string, sizeof(string), "Besitzer: %s", getPlayerName(playerid));
            UpdateDynamic3DTextLabelText(hInfo[houseid][hBesitzerText], COLOR_WHITE, string);
            DestroyPickup(hInfo[houseid][hPickup]);
            hInfo[houseid][hPickup] = CreatePickup(1272, 1, hInfo[houseid][henterx], hInfo[houseid][hentery], hInfo[houseid][henterz], -1);
            dini_Set(houseloc, "Besitzer", getPlayerName(playerid));
            return 1;
        }
        SendClientMessage(playerid, COLOR_RED, "Das Haus hat bereits einen Besitzer.");
        return 1;
    }
    SendClientMessage(playerid, COLOR_RED, "Du bist nicht in der Nähe eines Hauses.");
    return 1;
}
Bwandon is offline   Reply With Quote
Old 21/03/2020, 12:49 PM   #6
ImTobi
Big Clucker
 
ImTobi's Avatar
 
Join Date: Jun 2017
Posts: 144
Reputation: 3
Default Re: Buyhouse command not working

does it have to be complicated? can't i just change something in the buyhouse command that it works? because in an other command from me, it worked with the return after the loop, because i want to make all other commands and checks. Nothing against you, it's nice that you are helping, but i want to try to fix the buyhouse command first easy
ImTobi is offline   Reply With Quote
Old 21/03/2020, 12:57 PM   #7
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Buyhouse command not working

Two methods.
pawn Code:
// method 1:
for (...)
{
    if (IsPlayerInRangeOfPoint(..))
    {
        ...
       
        // found house, stop the code execution here
        return 1;
    }
}

// if not near in any house, it will reach this point
// send error
pawn Code:
// method 2:
new bool: in_range;

for (...)
{
    if (IsPlayerInRangeOfPoint(..))
    {
        ...

        // found house, break out of the loop
        in_range = true;
        break;
    }
}

if (!in_range)
{
    // if not near in any house, it will reach this point
    // send error
}
__________________
Calisthenics is offline   Reply With Quote
Old 21/03/2020, 01:24 PM   #8
ImTobi
Big Clucker
 
ImTobi's Avatar
 
Join Date: Jun 2017
Posts: 144
Reputation: 3
Default Re: Buyhouse command not working

Code:
CMD:buyhouse(playerid,params[])
{
	if(!IsPlayerLoggedIn(playerid))return notloggedin(playerid);
	new Float:x,Float:y,Float:z;
	new string[128];
	new file[128];
	new HouseID;
	GetPlayerPos(playerid, x, y, z);
	for(new i=0; i<sizeof(hInfo); i++)
	{
		if(IsPlayerInRangeOfPoint(playerid, 3, hInfo[i][henterx], hInfo[i][hentery], hInfo[i][henterz]))
		{
			if(!strlen(hInfo[i][hBesitzer]))
			{
				if(GetPlayerMoney(playerid) < hInfo[i][hPrice])return SendClientMessage(playerid, COLOR_RED, "Du hast nicht genügend Geld.");
				GivePlayerMoney(playerid, -hInfo[i][hPrice]);
				strcpy(hInfo[i][hBesitzer],getPlayerName(playerid),128);
				format(string,sizeof(string),"Besitzer: %s",getPlayerName(playerid));
				UpdateDynamic3DTextLabelText(hInfo[i][hBesitzerText], COLOR_WHITE, string);
				DestroyPickup(hInfo[i][hPickup]);
				hInfo[i][hPickup] = CreatePickup(1272, 1, hInfo[i][henterx], hInfo[i][hentery], hInfo[i][henterz], -1);
				HouseID = i;
				format(file,sizeof(file),"/houses/%i.ini",HouseID);
				if(fexist(file))
				{
						dini_Set(file,"Besitzer",getPlayerName(playerid));
				}
				return 1;
			}
			SendClientMessage(playerid, COLOR_RED, "Das Haus hat bereits einen Besitzer.");
			return 1;
		}
	}
	SendClientMessage(playerid, COLOR_RED, "Du bist nicht in der Nähe eines Hauses.");
	return 1;
}

it works like this.

Thanks for your replies, if something is bugging, i will use your codes
ImTobi is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
/buyhouse not working TheMafien Scripting Help 0 28/12/2015 06:35 PM
/buyhouse command isn't working.. Desi_Dude Scripting Help 0 12/12/2013 08:22 AM
[BUG] /buyhouse CMD is Not working. Ivander Scripting Help 1 06/09/2013 02:44 PM
[Help] /Buyhouse Command Matthew_Johnston Help Archive 3 02/04/2009 07:18 AM


All times are GMT. The time now is 09:16 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.