i cant get this loop functioning as i want...
#1

hey all,

i can't get this loop as i want it, i want it to loop through the "OtherCars[MAX_PLAYERS][4]", but instead it only checks OtherCars[playerid][0], and 1, 2, 3 is just gone, he doesnt checks them at all..., i already tried break; and continue;, both didnt work..

pawn Code:
CMD:park(playerid, params[])
{
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, COLOR_RED, "You aren't in any vehicle.");
    new vehicleid = GetPlayerVehicleID(playerid);
    for(new i; i < sizeof(OtherCars[]); i++)
    {
        if(vehicleid == OtherCars[playerid][i])
        {
            new Float:x, Float:y, Float:z, Float:angle;
            GetVehiclePos(OtherCars[playerid][i], x, y, z);
            GetVehicleZAngle(OtherCars[playerid][i], angle);
            SaveFloatToCarSlot(playerid, i, "XSpawn", x );
            SaveFloatToCarSlot(playerid, i, "YSpawn", y );
            SaveFloatToCarSlot(playerid, i, "ZSpawn", z );
            SaveFloatToCarSlot(playerid, i, "ASpawn", angle );
            SaveVehComponents(playerid, OtherCars[playerid][i], i);
            foreach(Player, p)
            {
                if(IsPlayerInVehicle(playerid, vehicleid))
                {
                    if(p != playerid && GetPlayerState(p) == PLAYER_STATE_DRIVER)
                    {
                        RemovePlayerFromVehicle(p);
                    }
                }
            }
            SetVehiclePosZAngle( OtherCars[playerid][i], x, y, z, angle );
            PutPlayerInVehicle(playerid, OtherCars[playerid][i], 0);
            SendClientMessage(playerid, COLOR_WHITE, "Car Succesfully Parked.");
            continue;
        }
        else return SendClientMessage(playerid, COLOR_RED, "This ain't your car.");
    }
    return 1;
}
whats wrong with this code that it only checks the first one of the othercars?
it keeps saying: this aint your car... except for the car that is at slot 0(OtherCars[playerid][0])

greets
Reply
#2

Don't put a return statement in a loop unless you explicitly want to break out of it.
Reply
#3

so you say i have to remove the "else return ...." thingy? or the continue? because i already tried it without continue, that didnt work.
Reply
#4

Quote:
Originally Posted by Apenmeeuw
View Post
so you say i have to remove the "else return ...." thingy? or the continue? because i already tried it without continue, that didnt work.
What he's saying is..
pawn Code:
else return SendClientMessage(playerid, COLOR_RED, "This ain't your car.");
Will break out of the loop. Making the function stop there.
Reply
#5

i now have changed it to this, and it works, but not exactly as i want it.

if i do /v park at for example vehicle 2(OtherCars[playerid][2]) than it says it like this:
This aint your vehicle.
This aint your vehicle.
vehicle successfully respawnd.
This aint your vehicle.

it shouldnt say those "this aint your car" messages at all, just the "vehicle respawned" message... whats wrong with this code:

pawn Code:
CMD:park(playerid, params[])
{
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, COLOR_RED, "You aren't in any vehicle.");
    new vehicleid = GetPlayerVehicleID(playerid);
    for(new i; i < sizeof(OtherCars[]); i++)
    {
        if(vehicleid == OtherCars[playerid][i])
        {
            new Float:x, Float:y, Float:z, Float:angle;
            GetVehiclePos(OtherCars[playerid][i], x, y, z);
            GetVehicleZAngle(OtherCars[playerid][i], angle);
            SaveFloatToCarSlot(playerid, i, "XSpawn", x );
            SaveFloatToCarSlot(playerid, i, "YSpawn", y );
            SaveFloatToCarSlot(playerid, i, "ZSpawn", z );
            SaveFloatToCarSlot(playerid, i, "ASpawn", angle );
            SaveVehComponents(playerid, OtherCars[playerid][i], i);
            foreach(Player, p)
            {
                if(IsPlayerInVehicle(playerid, vehicleid))
                {
                    if(p != playerid && GetPlayerState(p) == PLAYER_STATE_DRIVER)
                    {
                        RemovePlayerFromVehicle(p);
                    }
                }
            }
            SetVehiclePosZAngle( OtherCars[playerid][i], x, y, z, angle );
            PutPlayerInVehicle(playerid, OtherCars[playerid][i], 0);
            SendClientMessage(playerid, COLOR_WHITE, "Car Succesfully Parked.");
        }
        else SendClientMessage(playerid, COLOR_RED, "This ain't your car.");
    }
    return 1;
}
thanks
Reply
#6

Just remove the else...

If he has no cars, it's still going to say it however many times you have your loop loop. If you want it to say "this 'ain't' your car", you can add a counter.
pawn Code:
CMD:park(playerid, params[])
{
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, COLOR_RED, "You aren't in any vehicle.");
    new vehicleid = GetPlayerVehicleID(playerid), count=0;
    for(new i; i < sizeof(OtherCars[]); i++)
    {
        if(vehicleid == OtherCars[playerid][i])
        {
            new Float:x, Float:y, Float:z, Float:angle;
            GetVehiclePos(OtherCars[playerid][i], x, y, z);
            GetVehicleZAngle(OtherCars[playerid][i], angle);
            SaveFloatToCarSlot(playerid, i, "XSpawn", x );
            SaveFloatToCarSlot(playerid, i, "YSpawn", y );
            SaveFloatToCarSlot(playerid, i, "ZSpawn", z );
            SaveFloatToCarSlot(playerid, i, "ASpawn", angle );
            SaveVehComponents(playerid, OtherCars[playerid][i], i);
            foreach(Player, p)
            {
                if(IsPlayerInVehicle(playerid, vehicleid))
                {
                    if(p != playerid && GetPlayerState(p) == PLAYER_STATE_DRIVER)
                    {
                        RemovePlayerFromVehicle(p);
                    }
                }
            }
            SetVehiclePosZAngle( OtherCars[playerid][i], x, y, z, angle );
            PutPlayerInVehicle(playerid, OtherCars[playerid][i], 0);
            SendClientMessage(playerid, COLOR_WHITE, "Car Succesfully Parked.");
            count++;
            break;
        }
    }
    if(!count) SendClientMessage(playerid, COLOR_RED, "This isn't your car.");
    return 1;
}
That would detect if the car is the players. It checks if the count var = 0 (since it's only 1 if it's the players car) and sends an error message if it is (or whatever)
Reply
#7

ah that way , thanks, i got some issues with other commands too, ill fix those too the same way as this one, thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)