Vehicles Owned by 1 person, Script Bug
#1

I'm trying to make this simple, so while the code is elementary, all I need it to do is prevent other players from getting into the "gcars", yet this script causes ALL server cars to be unable to enter (except by "Jason_Budley" (server owner, me))

Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    new G_Check=GetPlayerVehicleID(playerid);
	if (G_Check==gcar||gcar2||gcar3||gcar4||gcar5||gcar6||gcar7||gcar8||gcar9)
	{
	    new PlayerName[24];
  		GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
    	if (strcmp(PlayerName,"Jason_Budley",true))
    	{
    	    ClearAnimations(playerid);
    	    return 1;
    	}
	}
	else
	{
	    return 1;
	}
	return 1;
}
Reply
#2

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    switch (vehicleid)
    {
        case gcar, gcar2, gcar3, gcar4, gcar5, gcar6, gcar7, gcar8, gcar9:
        {
            new PlayerName[24];
            GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
            if (strcmp(PlayerName,"Jason_Budley",true))
            {
                ClearAnimations(playerid);
                return 1;
            }
        }
    }

    return 1;
}
Reply
#3

Код:
C:\Users\Aerotactics' PC\Desktop\LS-RP\filterscripts\[RP]LH.pwn(235) : error 008: must be a constant expression; assumed zero
I get this error.
Reply
#4

On which line?
Reply
#5

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
On which line?
the "case ..." line
Reply
#6

My bad. Forgot that switch case needs constants, not variables.

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if ((vehicleid == gcar) || (vehicleid == gcar2) || (vehicleid == gcar3) || (vehicleid == gcar4) || (vehicleid == gcar5) || (vehicleid == gcar6) || (vehicleid == gcar7) || (vehicleid == gcar8) || (vehicleid == gcar9))
    {
            new PlayerName[24];
            GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
            if (strcmp(PlayerName,"Jason_Budley",true))
            {
                ClearAnimations(playerid);
                return 1;
            }
    }

    return 1;
}
If you used an array to store those vehicle id's, it would be simpler and shorter:
pawn Код:
new gcars[10];

// Somewhere:
gcars[0] = CreateVehicle(...
gcars[1] = Createvehicle(...
...
gcars[9] = CreateVehicle(...





public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    for (new i; i < sizeof(gcars); i++)
    {
        if (vehicleid == gcars[i])
        {
            new PlayerName[24];
            GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
            if (strcmp(PlayerName,"Jason_Budley",true))
            {
                ClearAnimations(playerid);
                return 1;
            }
        }
    }

    return 1;
}
If you increased the size of gcars to 20 for example, you don't even have to modify this code, as it loops through the entire array and checks if the vehicle you're trying to enter is one of those gcars.

With the first example, you would need to add 10 extra checks to that code and you risk another error to indicate your line has become too long.
Reply
#7

Yeah, I figured it out, thanks for the help.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)