Respawn All Vehicles
#1

pawn Code:
function RespawnAllVehicles()
{
      for( new i = i; i <= MAX_VEHICLES; i ++ )
      {
             if(i != INVALID_VEHICLE_ID)
             {
                     if(IsVehicleEmpty(i))
                     {
                              SetVehicleToRespawn(i);
                     }
             }
      }
      return 1;
}
When i used `RespawnAllVehicles` function in my scripts
Vehicles late too respawn and server lagged for 10second also players..
How to fix??
Reply
#2

You need to use foreach for this is. That way you won't need to loop over unused vehicleids so this loop will become a lot quicker.

But even then, lagging for 10 seconds seems unusually long. Are you sure this is your entire code? Because your loop seems weird: new i = i; should be new i = 0;
I'm pretty sure it shouldn't even compile with your current code.
Reply
#3

use this Code

PHP Code:
CMD:respawncars(playeridparams[])
{
    
//check if the player is a admin
    
LevelCheck(playerid4);
    for(new 
carscars MAX_VEHICLEScars++)
    {
        
LOOP_PLAYERS(i)
        {
            
PlayerPlaySound(i10570.00.00.0);
            if(
GetPlayerVehicleID(i) == cars)
            {
                if(
GetPlayerState(i) == PLAYER_STATE_DRIVER)
                {
                    if(
GetPlayerGAdminLevel(playerid) < GetPlayerGAdminLevel(i))
                    {
                        
SetVehicleToRespawn(cars);
                    }
                }
                else 
SetVehicleToRespawn(cars);
            }
            else 
SetVehicleToRespawn(cars);
        }
    }
    
GameTextForAll("~b~~h~~h~~h~Vehicles respawned"50003);
    new 
string[144];
    
format(stringsizeof(string), "You respawned all vehicles."ReturnPlayerName(playerid), playerid);
    
SendClientMessageToAll(COLOR_HOT_PINKstring);
    return 
1;

Reply
#4

Normally I ignore those code suggestions but I can't.
DON'T use that code, wtf. It's awful.

Quote:
Originally Posted by BsLOUAY
View Post
use this Code
PHP Code:
CMD:respawncars(playeridparams[])
{
    
//check if the player is a admin
    
LevelCheck(playerid4);
    for(new 
carscars MAX_VEHICLEScars++)
    {
        
LOOP_PLAYERS(i)
        {
            
PlayerPlaySound(i10570.00.00.0);
            if(
GetPlayerVehicleID(i) == cars)
            {
                if(
GetPlayerState(i) == PLAYER_STATE_DRIVER)
                {
                    if(
GetPlayerGAdminLevel(playerid) < GetPlayerGAdminLevel(i))
                    {
                        
SetVehicleToRespawn(cars);
                    }
                }
                else 
SetVehicleToRespawn(cars);
            }
            else 
SetVehicleToRespawn(cars);
        }
    }
    
GameTextForAll("~b~~h~~h~~h~Vehicles respawned"50003);
    new 
string[144];
    
format(stringsizeof(string), "You respawned all vehicles."ReturnPlayerName(playerid), playerid);
    
SendClientMessageToAll(COLOR_HOT_PINKstring);
    return 
1;

It depends on functions: LOOP_PLAYERS(), LevelCheck(), GetPlayerGAdminLevel(), ReturnPlayerName() which you probably don't have defined anywhere.
It doesn't use foreach so it loops through all possible vehicleids and, thus, is very inefficient.
Every single vehicleid iteration it loops through all players with LOOP_PLAYERS. If it's written in a similar manner, then I bet it doesn't use foreach as well, meaning you loop through way too many playerids.
Every single time you loop through a vehicleid you will play a sound which will just end up spamming players with useless noise for every vehicle.
Every time a player isn't in the vehicle that is currently being iterated - that vehicle will be respawned. Meaning that you will respawn the same vehicle multiple (possibly hundreds of) times before a single loop is even finished.
Then you will also respawn the current vehicle every time a player isn't a driver but is inside the vehicle for whatever reason.
And you will also respawn the vehicle if the player is inside of it, is the driver and is a higher admin level than the person that wrote the command? That just doesn't make sense at all.

You seriously need to rethink your code, BsLOUAY.


Vizi10, just redo your function with foreach (from YSI or whatever). It will work fine.

PHP Code:
RespawnAllVehicles()
{
    foreach(new 
vehicleid Vehicle)
     {
        if(
IsVehicleEmpty(vehicleid))
        {
            
SetVehicleToRespawn(vehicleid);
        }
    }
    return 
1;

Reply
#5

Thank you AdamsLT, fixed with foreach.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)