Change car
#1

I am trying to detect when a player changes car, and if it's a banned car, then destroy it. If they are on foot and spawn the car, then it works. If they are already in a car and they spawn a banned car then it doesn't, any ideas?

Код:
public OnPlayerUpdate(playerid)
{
    if(playerid==robber)
	{
		if(robbercar != GetPlayerVehicleID(playerid))
		{
			OnPlayerVehicleChange(playerid,GetPlayerVehicleID(playerid));
		}
	}
}

OnPlayerVehicleChange(playerid,vehicleid)
{
	switch(GetVehicleModel(vehicleid))
	{
		case 592: RestrictedCar(playerid,"Andromada");
	}
	robbercar=GetPlayerVehicleID(playerid);
}
When on foot, OnPlayerVehicleChange is never called.

The robber value is just a variable for a playerid, as the vehicles are only restricted with this player.
Reply
#2

I assume what you are trying to do is prevent certain players from accessing certain vehicles.

I have done this myself, and I would suggest to leave the OnPlayerUpdate callback and use the OnPlayerStateChange (called every time the player changes state) or the OnPlayerEnterVehicle (called when a player presses their "enter vehicle" key) callbacks instead. Also please explain how your code is supposed to work, in particular the "playerid==robber" part.
Reply
#3

Yes that's right.

I was using OnPlayerStateChange originally, but this doesn't get called when a player changes from one vehicle to another without exiting it.

I will post the whole of my relevant code, if it helps, thanks.
Reply
#4

I understand why you would not use OnPlayerStateChange, as you are aware, it is only called once the player is sitting in the drivers seat, ready to drive. OnPlayerEnterVehicle(playerid, vehicleid) on the other hand is called when they give the command to enter the vehicle, ie it will be called before they actually start moving to get in the vehicle, therefore by using this, you make your server faster, because it doesn't have to do this check every 30ms, only when the player enters a vehicle.

If the code on your server literally is playerid==robber, I suggest you change it, because this can only cope with one robber.

In my experience, I would suspect that your problem would be here
pawn Код:
if(robbercar != GetPlayerVehicleID(playerid))
If you switch to OnPlayerEnterVehicle, it should work, but this won't, because it does the check every 30ms, and if the player is not in a vehicle, your code will set robbercar to INVLAID_VEHICLE_ID, which is what GetPlayerVehicleID returns when the player is not in a vehicle.
Reply
#5

Thanks so much EnforcerDon, I not entirely sure if I know what you mean, but the return value of the INVALID_VEHICLE_ID makes sense. I tried the OnPlayerEnterVehicle but it didn't work when changing seats.

I've gotten round it by ensuring the robber is in a vehicle using this, does this look ok to you, or is there a better/more efficient way around it?

Код:
public OnPlayerUpdate(playerid)
{
    if(playerid==robber && IsPlayerInAnyVehicle(playerid))
	{
		switch(GetVehicleModel(GetPlayerVehicleID(playerid)))
		{
			case 592: RestrictedCar(playerid,"Andromada");
		}
	}
	return 1;
}
Could I now safely make the check if they are in a different vehicle, or is that pointless, if I'm going to destroy it if it's banned?

Thanks again
Reply
#6

Quote:
Originally Posted by alibalicharlton
Посмотреть сообщение
Thanks so much EnforcerDon, I not entirely sure if I know what you mean, but the return value of the INVALID_VEHICLE_ID makes sense. I tried the OnPlayerEnterVehicle but it didn't work when changing seats.

I've gotten round it by ensuring the robber is in a vehicle using this, does this look ok to you, or is there a better/more efficient way around it?

Код:
public OnPlayerUpdate(playerid)
{
    if(playerid==robber && IsPlayerInAnyVehicle(playerid))
	{
		switch(GetVehicleModel(GetPlayerVehicleID(playerid)))
		{
			case 592: RestrictedCar(playerid,"Andromada");
		}
	}
	return 1;
}
Could I now safely make the check if they are in a different vehicle, or is that pointless, if I'm going to destroy it if it's banned?

Thanks again
OnPlayerEnterVehicle would still be much more efficient if you could get it to work, but you would have to change your RestrictedCar function to take a vehicleid instead of a playerid, (or just take both) because you would be calling it before the player was actually in it, if you get what I mean, as such. It didn't work because when RestrictedCar calls the DestroyVehicle function, it passes it an invalid vehicle id, due to the player vehicle id being an invalid vehicle.

pawn Код:
public OnPlayerEnterVehicel(playerid, vehicleid)
{
    if(playerid==robber)
    {
        if(GetVehicleModel(vehicleid) == 592)
        {
            RestrictedCar(playerid,vehicleid,"Andromada");
        }
    }
}
** untested
RestrictedCar will be called every time a robber tries to enter an Andromada.

And what exactly do you mean when you say "Could I now safely make the check if they are in a different vehicle", please try to be a little more specific, because I'm missing your point.

If you have any other problems, let me know.

BTW, what kind of server are you working on?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)