How to use loop and return
#1

Hello guys!

I ran into trouble...
I have all these commands :

pawn Код:
CMD:npirkti(playerid,params[])
{
    for(new h; h < MAX_HOUSES; h++)
    {
        new pName[MAX_PLAYER_NAME];
        GetPlayerName(playerid, pName, sizeof(pName));
        if(strcmp(HouseInfo[h][hOwnerName], pName, false ) == 0)
        {
            SendClientMessage(playerid, COL_RED, "Tu jau turi namus!");
            return 1;
        }
        if(IsPlayerInRangeOfPoint(playerid, 5, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY] , HouseInfo[h][hEnterZ]))
        {
            if(HouseInfo[h][hOwned] == 0)
            {
                if(GetPlayerMoney(playerid) > HouseInfo[h][hPrice])
                {
                    strmid(HouseInfo[h][hOwnerName], pName, 0, strlen(pName),MAX_PLAYER_NAME);
                    HouseInfo[h][hOwned] = 1;
                    GivePlayerMoney(playerid,-HouseInfo[h][hPrice]);
                    SendClientMessage(playerid, COL_RED,"Sveikiname, nusipirkote nama! Visas komandas rasite /namai.");
                    HouseInfo[h][hIsRented] = 0;
                    new labelstring[500];
                    DestroyDynamic3DTextLabel(HouseInfo[h][hLabel]);
                    DestroyDynamicMapIcon(HouseInfo[h][hIcon]);
                    DestroyDynamicPickup(HouseInfo[h][hPickup]);

                    format(labelstring, sizeof(labelstring),""TCCYAN"Savininkas: "TCRED"%s\n"TCCYAN"Nuoma: "TCRED"%s\n"TCCYAN"Nuomos kaina: "TCRED"%d\n"TCCYAN"Adresas: "TCRED"%s", HouseInfo[h][hOwnerName],HouseInfo[h][hRenter],HouseInfo[h][hRentPrice],HouseInfo[h][hAddress]);
                    HouseInfo[h][hLabel] = CreateDynamic3DTextLabel(labelstring, COL_GREEN, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ], 20);
                    HouseInfo[h][hPickup] = CreateDynamicPickup(1272, 1, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ]);
                    HouseInfo[h][hIcon] = CreateDynamicMapIcon(HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ], 32, 0,0);
                    SaveHouseData(h);
                }
                else SendClientMessage(playerid, COL_RED, "Jums neuztenka pinigu ipirkti siam namui!");
            }
            else SendClientMessage(playerid, COL_RED, "Sis namas jau nupirktas!Jei jis nuomuojamas gali ji nuomuoti.");  
        }
        else SendClientMessage(playerid, COL_RED, "Jus nesate prie parduodamo namo!");
    }
    return 1;
}


CMD:nparduoti(playerid,params[])
{
    new pName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pName, sizeof(pName));
    for(new h;h < MAX_HOUSES;h++)
    {
        if(strcmp(HouseInfo[h][hOwnerName], pName, false ) == 1)
        {
            SendClientMessage(playerid, COL_RED, "Tu neturi namo , kuri galetum parduot!");
            return 1;
        }
        if(IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ]))
        {
            if(HouseInfo[h][hOwned] == 1)
            {
                if(strcmp(HouseInfo[h][hOwnerName], pName, false ) == 0)
                {
                    if(HouseInfo[h][hGIsOccupied] != 0)
                    {
                        LinkVehicleToInterior(HouseInfo[h][hGIsOccupied], 0);
                        SetVehicleVirtualWorld(HouseInfo[h][hGIsOccupied], 0);
                        SetVehiclePos(HouseInfo[h][hGIsOccupied], HouseInfo[h][hGEnterX], HouseInfo[h][hGEnterX], HouseInfo[h][hGEnterX]);
                        DestroyDynamic3DTextLabel(HouseInfo[h][hGLabel]);
                        SetVehicleZAngle(HouseInfo[h][hGIsOccupied], HouseInfo[h][hGAngle]);
                    }
                    GivePlayerMoney(playerid,HouseInfo[h][hPrice]+HouseInfo[h][hMoney]);
                    GivePlayerWeapon(playerid, HouseInfo[h][hGun1], HouseInfo[h][hAmmo1]);
                    GivePlayerWeapon(playerid, HouseInfo[h][hGun2], HouseInfo[h][hAmmo2]);
                    strmid(HouseInfo[h][hOwnerName], "Parduodamas!", 0, strlen("Parduodamas!"), 20);
                    HouseInfo[h][hOwned] = 0;
                    HouseInfo[h][hLocked] = 1;
                    HouseInfo[h][hRentPrice] = 0;
                    strmid(HouseInfo[h][hRenter],"Nera",0,strlen("Nera"),6);
                    HouseInfo[h][hIsRented] = 0;
                    HouseInfo[h][hGun1] = 0;
                    HouseInfo[h][hGun2] = 0;
                    HouseInfo[h][hAmmo1] = 0;
                    HouseInfo[h][hAmmo2] = 0;
                    HouseInfo[h][hhasAGarage] = 0;
                    HouseInfo[h][hGIsOccupied] = 0;
                    HouseInfo[h][hGIsLocked] = 1;

                    DestroyDynamicPickup(HouseInfo[h][hPickup]);
                    DestroyDynamic3DTextLabel(HouseInfo[h][hLabel]);
                    DestroyDynamic3DTextLabel(HouseInfo[h][hGLabel]);
                    DestroyDynamicMapIcon(HouseInfo[h][hIcon]);

                    new labelstring[500];
                    format(labelstring,sizeof(labelstring),""TCRED"Patalpa parduodama!\n"TCCYAN"Kaina: "TCRED"%d LT\n"TCCYAN"Adresas: "TCRED"%s\n"TCCYAN"Informacija: "TCRED"%s",HouseInfo[h][hPrice],HouseInfo[h][hAddress],HouseInfo[h][hDescription]);
                    HouseInfo[h][hLabel] = CreateDynamic3DTextLabel(labelstring, COL_GREEN, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ], 20);
                    HouseInfo[h][hPickup] = CreateDynamicPickup(1273, 1, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ]);
                    HouseInfo[h][hIcon] = CreateDynamicMapIcon(HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ], 31, 0,0);
                }
                else SendClientMessage(playerid, COL_RED, "Tai ne tavo namas!");
            }
            else SendClientMessage(playerid, COL_RED, "Sis namas nera nupirktas!");
        }
        else SendClientMessage(playerid, COL_RED, "Turi buti prie namo duru!");
    }
    return 1;
}

CMD:namai(playerid,params[])
{
    for(new h;h < MAX_HOUSES; h++)
    {
        new pName[MAX_PLAYER_NAME];
        GetPlayerName(playerid, pName, sizeof(pName));
        if((IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ]) || IsPlayerInRangeOfPoint(playerid, 20, HouseInfo[h][hExitX], HouseInfo[h][hExitY], HouseInfo[h][hExitZ])))
        {
            if(strcmp(HouseInfo[h][hOwnerName], pName, false) == 0)
            {
                ShowPlayerDialog(playerid, DIALOG_HOUSE_COMMANDS, DIALOG_STYLE_LIST, "Namu komandos", "Namu spyna\nSusimoketi Mokescius\nNuoma\nNuomos kaina\nIsmesti nuomininka\nIsspirti visus", "Gerai", "Iseiti");
                return 1;
            }
            else if(strcmp(HouseInfo[h][hRenter], pName, false) == 0)
            {
                ShowPlayerDialog(playerid, DIALOG_HOUSE_RENTER_COMMANDS, DIALOG_STYLE_LIST, "Namu komandos", "Namu spyna\nIsspirti visus", "Gerai", "Iseiti");
                return 1;
            }
        }
        else SendClientMessage(playerid, COL_RED, "Tu ne savo namuose arba ne prie ju!");
    }
    return 1;
}

CMD:nuomuoti(playerid,params[])
{
    for(new h;h < MAX_HOUSES;h++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ]))
        {
            if(HouseInfo[h][hIsRented] == 0)
            {
                if(HouseInfo[h][hIsRentable] == 1)
                {
                    new pName[MAX_PLAYER_NAME];
                    GetPlayerName(playerid, pName, sizeof(pName));
                    if(strcmp(HouseInfo[h][hOwnerName], pName, false) == 0)
                    {
                        if(GetPlayerMoney(playerid) >= HouseInfo[h][hRentPrice])
                        {
                            strmid(HouseInfo[h][hRenter], pName, 0, strlen(pName),MAX_PLAYER_NAME);
                            HouseInfo[h][hIsRented] = 1;
                            GivePlayerMoney(playerid,-HouseInfo[h][hRentPrice]);
                            SendClientMessage(playerid, COL_GREEN, "Sveikiname isinuomavai si nama!Visas komandas rasi /namai.");

                            new labelstring[500];
                            DestroyDynamic3DTextLabel(HouseInfo[h][hLabel]);

                            format(labelstring, sizeof(labelstring),""TCCYAN"Savininkas: "TCRED"%s\n"TCCYAN"Nuoma: "TCRED"%s\n"TCCYAN"Nuomos kaina: "TCRED"%d\n"TCCYAN"Adresas: "TCRED"%s", HouseInfo[h][hOwnerName],HouseInfo[h][hRenter],HouseInfo[h][hRentPrice],HouseInfo[h][hAddress]);
                            HouseInfo[h][hLabel] = CreateDynamic3DTextLabel(labelstring, COL_GREEN, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ], 20);
                            SaveHouseData(h);
                        }
                        else SendClientMessage(playerid, COL_RED, "Tau nepakanka pinigu siam namui nuomuotis!");
                    }
                    else SendClientMessage(playerid, COL_RED, "Negalite nuomuotis savo paties namo!");
                }
                else SendClientMessage(playerid, COL_RED, "Sis namas nera nuomuojamas!");
            }
            else SendClientMessage(playerid, COL_RED, "Sis namas jau nuomuojamas!");
        }
        else SendClientMessage(playerid, COL_RED, "Tu ne prie namo duru!");
    }
    return 1;
}

CMD:nesinuomuoti(playerid,params[])
{
    for(new h;h < MAX_HOUSES;h++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ]))
        {
            new pName[MAX_PLAYER_NAME];
            GetPlayerName(playerid, pName, sizeof(pName));
            if(strcmp(HouseInfo[h][hRenter], pName, false) == 0)
            {
                strmid(HouseInfo[h][hRenter], "Nera", 0, strlen("Nera"),6);
                HouseInfo[h][hIsRented] = 0;
                SendClientMessage(playerid, COL_GREEN, "Jus sekmingai atsaukete namo nuoma!");

                new labelstring[500];
                DestroyDynamic3DTextLabel(HouseInfo[h][hLabel]);

                format(labelstring, sizeof(labelstring),""TCCYAN"Savininkas: "TCRED"%s\n"TCCYAN"Nuoma: "TCRED"%s\n"TCCYAN"Nuomos kaina: "TCRED"%d\n"TCCYAN"Adresas: "TCRED"%s", HouseInfo[h][hOwnerName],HouseInfo[h][hRenter],HouseInfo[h][hRentPrice],HouseInfo[h][hAddress]);
                HouseInfo[h][hLabel] = CreateDynamic3DTextLabel(labelstring, COL_GREEN, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ], 20);
                SaveHouseData(h);
            }
            else SendClientMessage(playerid, COL_RED, "Jus jau ir taip nesinuomuojate jokio namo!");
        }
        else SendClientMessage(playerid, COL_RED, "Jus nesate arti namo duru!");
    }
    return 1;
}

CMD:ngkurti(playerid,params[])
{

    new pName[MAX_PLAYER_NAME],Float:X,Float:Y,Float:Z,Float:Angle;
    GetPlayerName(playerid, pName, sizeof(pName));
    GetPlayerPos(playerid, X, Y, Z);
    GetPlayerFacingAngle(playerid,Angle);
    for(new h;h < MAX_HOUSES;h++)
    {
        if(strcmp(HouseInfo[h][hOwnerName], pName, false ) == 0)
        {
            if(IsPlayerInRangeOfPoint(playerid, 10, HouseInfo[h][hEnterX], HouseInfo[h][hEnterY], HouseInfo[h][hEnterZ]))
            {
                if(HouseInfo[h][hhasAGarage] == 0)
                {
                    new garagelabel[200];
                    HouseInfo[h][hGEnterX] = X;
                    HouseInfo[h][hGEnterY] = Y;
                    HouseInfo[h][hGEnterZ] = Z;
                    HouseInfo[h][hGExitX] = 616.7820;
                    HouseInfo[h][hGExitY] = -74.8151;
                    HouseInfo[h][hGExitZ] = 997.6350;
                    HouseInfo[h][hGAngle] = Angle;
                    format(garagelabel, sizeof(garagelabel),""TCRED"Garazas ("TCCYAN"Ieiti iseit ([C] arba [H]"TCRED")\n"TCCYAN"Savininkas: "TCRED"%s", HouseInfo[h][hOwnerName]);
                    HouseInfo[h][hGLabel] = CreateDynamic3DTextLabel(garagelabel, COL_GREEN, HouseInfo[h][hGEnterX], HouseInfo[h][hGEnterY], HouseInfo[h][hGEnterZ], 20);
                    HouseInfo[h][hhasAGarage] = 1;
                    HouseInfo[h][hGIsLocked] = 1;
                    HouseInfo[h][hGIsOccupied] = 0;
                    SaveHouseData(h);
                }
                else SendClientMessage(playerid, COL_RED, "Jusu namas jau turi irengta garaza!");
            }
            else SendClientMessage(playerid, COL_RED, "Jus turite buti arti namo!");
        }
        else SendClientMessage(playerid, COL_RED, "Jus neturite namo!");
    }
    return 1;
}

CMD:ngnaikinti(playerid,params[])
{
    new pName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pName, sizeof(pName));
    for(new h;h < MAX_HOUSES;h++)
    {
        if(strcmp(HouseInfo[h][hOwnerName], pName, false ) == 0 || PlayerInfo[playerid][pAdmin] == 5)
        {
            if(IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[h][hGEnterX], HouseInfo[h][hGEnterY], HouseInfo[h][hGEnterZ]))
            {
                if(HouseInfo[h][hhasAGarage] == 1)
                {
                    if(HouseInfo[h][hGIsOccupied] != 0)
                    {
                        LinkVehicleToInterior(HouseInfo[h][hGIsOccupied], 0);
                        SetVehicleVirtualWorld(HouseInfo[h][hGIsOccupied], 0);
                        SetVehiclePos(HouseInfo[h][hGIsOccupied], HouseInfo[h][hGEnterX], HouseInfo[h][hGEnterX], HouseInfo[h][hGEnterX]);
                        SetVehicleZAngle(HouseInfo[h][hGIsOccupied], HouseInfo[h][hGAngle]);
                    }
                    HouseInfo[h][hhasAGarage] = 0;
                    HouseInfo[h][hGIsLocked] = 0;
                    HouseInfo[h][hGIsOccupied] = 0;
                    HouseInfo[h][hGEnterX] = 0;
                    HouseInfo[h][hGEnterY] = 0;
                    HouseInfo[h][hGEnterZ] = 0;
                    DestroyDynamic3DTextLabel(HouseInfo[h][hGLabel]);
                    SaveHouseData(h);
                }
                else SendClientMessage(playerid, COL_RED, "Jusu namas neturi irengto garazo!");
            }
            else SendClientMessage(playerid, COL_RED, "Jus turite buti arti namo!");
        }
        else SendClientMessage(playerid, COL_RED, "Jus nesate sio namo savininkas!");
    }
    return 1;
}

CMD:garazas(playerid,params[])
{
    for(new h;h < MAX_HOUSES; h++)
    {
        new pName[MAX_PLAYER_NAME];
        GetPlayerName(playerid, pName, sizeof(pName));
        if((IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[h][hGEnterX], HouseInfo[h][hGEnterY], HouseInfo[h][hGEnterZ])))
        {
            if(strcmp(HouseInfo[h][hOwnerName], pName, false) == 0 || strcmp(HouseInfo[h][hRenter], pName, false) == 0)
            {
                ShowPlayerDialog(playerid, DIALOG_HOUSE_GARAGE_LOCK, DIALOG_STYLE_MSGBOX, "Garazas", "Ka norite daryti su savo garazu?", "Atrakinti", "Uzrakinti");
            }
        }
        else if(IsPlayerInRangeOfPoint(playerid, 20, HouseInfo[h][hGExitX], HouseInfo[h][hGExitY], HouseInfo[h][hGExitZ]) && IsPlayerInAnyVehicle(playerid))
        {
            ShowPlayerDialog(playerid, DIALOG_HOUSE_GARAGE_CAR_FIX, DIALOG_STYLE_LIST, "Garazo komandos", "Pataisyti masina", "Rinktis", "Atsaukti");
        }
        else SendClientMessage(playerid, COL_RED, "Tu ne savo garaze arba arba ne prie jo!"); return 1;
    }
    return 1;
}
And the loop is not working correctly - sometimes its says that im not near the house , sometimes its say that i dont have the house even if i do.

Whats wrong?

And the chat log:
Код:
[16:52:29] {FFFFFF}SA-MP {B9C9BF}0.3z {FFFFFF}Started

[16:52:32] Connecting to 94.244.89.42:7777...

[16:52:32] Connected. Joining the game...

[16:52:32] {00ff66}[{ffffff}Savininkas{00ff66}] {ffff00}Interneto_Tiekejas {66ffff}prisijunge prie serverio!

[16:52:35] Connected to {B9C9BF}Sa-Mp Server for Testing Purpouses

[16:52:46] Turi buti prie namo duru!

[16:52:46] Tu neturi namo , kuri galetum parduot!

[16:52:49] Turi buti prie namo duru!

[16:52:49] Tu neturi namo , kuri galetum parduot!

[16:52:56] Turi buti prie namo duru!

[16:52:56] Tu neturi namo , kuri galetum parduot!

[16:53:05] Sis namas nera nupirktas!

[16:53:05] Tu neturi namo , kuri galetum parduot!

[16:53:13] [Serveris]: Tokios komandos nera.Prasome pabandyti /pagalba!

[16:53:16] Tu ne savo namuose arba ne prie ju!

[16:53:28] Tu ne savo namuose arba ne prie ju!

[16:53:28] Tu ne savo namuose arba ne prie ju!

[16:53:33] Tu ne savo namuose arba ne prie ju!

[16:53:33] Tu ne savo namuose arba ne prie ju!

[16:53:40] Tu ne savo namuose arba ne prie ju!

[16:53:40] Tu ne savo namuose arba ne prie ju!
Reply
#2

Stop the loop at end of checks? e.g
pawn Код:
for (new i; i < 1000; ++ i)
{
    if (IsPlayerConnected(i))
    {
        if (cool[i] == true)
        {
            if (hax0r[i] == false)
            {
                DoSomething();
                break; // End the loop as player is found
            }
            else
            {
                Ban(i);
                continue; // Skip current "i" to continue looping to search the correct player.
            }
        }
        else
        {
            cool[i] = true;
            continue; // Skip current "i" to continue looping to search the correct player.
        }
    }
    return 1;
}
I don't know if that really helps, but what I see your problem is you didn't end the loop when the player has found the right house.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)