Normally I ignore those code suggestions but I can't.
DON'T use that code, wtf. It's awful.
Quote:
Originally Posted by BsLOUAY
use this Code
PHP Code:
CMD:respawncars(playerid, params[])
{
//check if the player is a admin
LevelCheck(playerid, 4);
for(new cars; cars < MAX_VEHICLES; cars++)
{
LOOP_PLAYERS(i)
{
PlayerPlaySound(i, 1057, 0.0, 0.0, 0.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", 5000, 3);
new string[144];
format(string, sizeof(string), "You respawned all vehicles.", ReturnPlayerName(playerid), playerid);
SendClientMessageToAll(COLOR_HOT_PINK, string);
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;
}