vehicles empty.
#1

How can I detect an empty vehicle ? I would like to make a command to delete empty vehicles but I can't find a function, how does someone could help me ?.
Reply
#2

with zcmd:
pawn Код:
CMD:cleanvehicles(playerid, params[])
{
       for(new i = 0; i < MAX_PLAYERS; i++)
       {
               for(new a = 0; a < MAX_VEHICLES; a++)
               {
                       if(!IsPlayerInVehicle(i, a) && i != INVALID_PLAYER_ID && a != INVALID_VEHICLE_ID)
                       {
                                 SetVehicleToRespawn(a);
                                 SendClientMessageToAll(-1, "Unused vehicles successfully respawned"); // debugging
                       }
               }
        }
        return 1;
}
edit it and add admin's variable check as your wish.
Reply
#3

The above code will respawn every vehicle if playerid 0 isn't inside any of them.
It will also respawn the same vehicle for every player not inside a vehicle.
Also, the additional checks never work since it's looping through integer values from 0 to MAX_PLAYERS (usually 50) and MAX_VEHICLES (usually 2000).
Both INVALID_xxx values are set to 65535, which will never be reached and can be removed from that code.

And it will spam the chatbox as well.

pawn Код:
CMD:cleanvehicles(playerid, params[])
{
    // Setup local variables
    new bool:PlayerInsideVehicle = false;

    // Loop through all vehicles (first vehicle-id is 1, not 0)
    for(new vid = 1; vid < MAX_VEHICLES; vid++)
    {
        // If this vehicle doesn't have a model, it doesn't exist and the loop continues with the next vehicle, skipping the rest of the code
        if (GetVehicleModel(vid) == 0) continue;

        // Loop through all players
        for(new i = 0; i < MAX_PLAYERS; i++)
        {
            // Check if this player is connected
            if (IsPlayerConnected(i)
            {
                // If this player is connected, check if he's inside the current vehicle
                if(IsPlayerInVehicle(i, vid))
                {
                    // Current vehicle is occupied by at least one player
                    PlayerInsideVehicle = true;
                }
            }
        }

        // If no player is inside this vehicle, respawn it (if there is at least one player inside it, don't do anything)
        if (PlayerInsideVehicle == false)
        {
            SetVehicleToRespawn(vid); // You may also put "DestroyVehicle(vid);" on this line to delete them instead of respawning
        }

        // Re-initialize the variable and proceed with the next vehicle
        PlayerInsideVehicle = false;
    }

    // Send a message to all players to let them know unoccupied vehicles have been respawned
    SendClientMessageToAll(-1, "Unused vehicles successfully respawned");

    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)