[BEGINNER] Need help destroying vehicles
#1

Hello,
I have started writing my first script from scratch recently, and I need help with two functions:
Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/freecar", cmdtext, true, 8) == 0)
	{
		new Float:x, Float:y, Float:z;
		GetPlayerPos(playerid,x,y,z);
		CreateVehicle(542,x,y,z,90,-1,-1,0);
		return 1;
	}
	return 0;
}
Код:
public OnPlayerExitVehicle(playerid, vehicleid)
{
	if (GetPlayerVehicleSeat(playerid) == 0)
	{
	    DestroyVehicle(vehicleid);
	    return 1;
	}
	return 0;
}
So my question is: is there any easy way to do that only cars which were created with /freecar command would be destroyed upon exit? At the moment any vehicle is destroyed and not respawning.
Reply
#2

@Ralfie
Thank you very much, it works! I didn't test it with more than one player, but I hope it will work.

Also, if you could add comments explaining how this code works or give me a link to a tutorial where it is explained I would greatly appreciate =]
Reply
#3

Hello,

pawn Код:
new FreeCar[MAX_PLAYERS] = INVALID_VEHICLE_ID; //this is a global variable, LINK 1.

OnPlayerConnect(playerid) FreeCar[playerid] = -1;
OnPlayerDisconnect(playerid) FreeCar[playerid] = -1;

//im setting the global variable to -1 so its invalid when a player connects, and disconnects, this should be stated when dealing with multiple players so it wont mess up between them...

if (strcmp("/freecar", cmdtext, true, 8) == 0) //thats ur cmd
    {
        new Float:x, Float:y, Float:z;
        GetPlayerPos(playerid,x,y,z);
        FreeCar[playerid] = CreateVehicle(542,x,y,z,90,-1,-1,0); //here we are specifying that variable to this spawned car of this player by adding 'playerid' next to it

        return 1;
    }

public OnPlayerExitVehicle(playerid, vehicleid)
{
    if (GetPlayerVehicleSeat(playerid) == 0 && FreeCar[playerid] != -1) //when the player exits the vehicle, and his state is as a driver and his car IS NOT equal to -1(as when he connected) this car gets removed :]
    {
        DestroyVehicle(vehicleid);
            FreeCar[playerid] = -1;  //then this sets the variable to -1 again so when he/she types the cmd again, the variable will be specified to the new spawned car.
        return 1;
    }
    return 0;
}
LINK 1: https://sampwiki.blast.hk/wiki/Scripting_Basics#global
Reply
#4

Thank you very much for your help!
+rep =]

/thread
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)