Help foreach Player
#1

[22:36:08] [debug] Run time error 4: "Array index out of bounds"
[22:36:08] [debug] Accessing element at index 500 past array upper bound 499

I use

foreach(new i: Player)


can I increase MAX_PLAYERS ??
Reply
#2

Put this at the top of your script
pawn Код:
#define MAX_PLAYERS 500
Reply
#3

It on a_samp include

If I #define MAX_PLAYERS 501 It wrong

[22:36:08] [debug] Run time error 4: "Array index out of bounds"
[22:36:08] [debug] Accessing element at index 501 past array upper bound 500
Reply
#4

Show the code where you are getting the error.
Reply
#5

Код:
public OnVehicleSpawn(vehicleid) {
	#if defined DEBUG
	    printf("[debug] OnVehicleSpawn(%d)", vehicleid);
	#endif

	switch(GetVehicleModel(vehicleid)) {
		case 427, 428, 432, 601, 528: SetVehicleHealth(vehicleid, 5000.0); // Enforcer, Securicar, Rhino, SWAT Tank, FBI truck - this is the armour plating dream come true.
	}

	if(vehicleid != 509 && vehicleid != 481 && vehicleid != 510)
	{
	    SetVehicleParamsEx(vehicleid, 0, 0, 0, 0, 0, 0, 0);
		if(IsPackerCar(vehicleid))
		{
	 		if(IsWoodVeh[vehicleid])
			{
				IsWoodVeh[vehicleid] = 0;
				if(IsValidDynamicObject(WoodOnVeh[vehicleid]))
				{
					DestroyDynamicObject(WoodOnVeh[vehicleid]);
				}
			}
		}
		if(IsGasCar(vehicleid))
		{
	 		if(IsGasOnVeh[vehicleid])
			{
				IsGasOnVeh[vehicleid] = 0;
				if(IsValidDynamicObject(GasOnVeh[vehicleid]))
				{
					DestroyDynamicObject(GasOnVeh[vehicleid]);
				}
			}
		}
  		foreach(new i: Player)
		{
 		    if(vehicleid == PlayerInfo[i][pVeh])
		    {
		        if(PlayerInfo[i][vLocked]) SetVehicleParamsEx(vehicleid, 0, 0, 0, 1, 0, 0, 0);
	            ChangeVehiclePaintjob(PlayerInfo[i][pVeh], PlayerInfo[i][vPJ]);
	            SetVehicleVirtualWorld(PlayerInfo[i][pVeh], PlayerInfo[i][pVehVW]);
				for(new x=0; x<14; i++)
				{
				    AddVehicleComponent(PlayerInfo[i][pVeh], PlayerInfo[i][pVehMod][x]);
				}
		    }
  		    if(vehicleid == PlayerInfo[i][pVVeh])
		    {
		        if(PlayerInfo[i][vvLocked]) SetVehicleParamsEx(vehicleid, 0, 0, 0, 1, 0, 0, 0);
	            ChangeVehiclePaintjob(PlayerInfo[i][pVVeh], PlayerInfo[i][vvPJ]);
	            SetVehicleVirtualWorld(PlayerInfo[i][pVVeh], PlayerInfo[i][pVVehVW]);
				for(new x=0; x<14; i++)
				{
				    AddVehicleComponent(PlayerInfo[i][pVVeh], PlayerInfo[i][pVVehMod][x]);
				}
		    }
		    if(vehicleid == RentCarKey[i])
		    {
		        if(RentCarLock[i] == 1) SetVehicleParamsEx(vehicleid, 0, 0, 0, 1, 0, 0, 0);
		    }
		}

    }
	return 1;
}
Reply
#6

Are you sure that the problem is foreach?
Use this inside foreach, at the top:
pawn Код:
printf("playerid: %i", i);
Make sure you're not using 'MAX_PLAYERS' when declaring these variables:
pawn Код:
new IsGasOnVeh[MAX_VEHICLES],
    IsWoodVeh[MAX_VEHICLES];
Check this function too, and make sure they're declared with 'MAX_VEHICLES':
pawn Код:
IsPackerCar(vehicleid)
Reply
#7

It looks like

pawn Код:
for(new i=0; i <= MAX_PLAYERS; i++)
but must be

pawn Код:
for(new i=0; i < MAX_PLAYERS; i++)
Reply
#8

Quote:
Originally Posted by Jefff
Посмотреть сообщение
It looks like

pawn Код:
for(new i=0; i <= MAX_PLAYERS; i++)
but must be

pawn Код:
for(new i=0; i < MAX_PLAYERS; i++)
He's using foreach, not a standard for-loop for players
Reply
#9

To increase the size of MAX_PLAYERS you can do like this.

pawn Код:
#undef MAX_PLAYERS;
#define MAX_PLAYERS   501
Reply
#10

pawn Код:
for(new x=0; x<14; i++)
i++?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)