SA-MP Forums Archive
[BEGINNER] Need help destroying vehicles - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: [BEGINNER] Need help destroying vehicles (/showthread.php?tid=503685)



[BEGINNER] Need help destroying vehicles - dovys11 - 30.03.2014

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.


Re: [BEGINNER] Need help destroying vehicles - dovys11 - 30.03.2014

@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 =]


Re: [BEGINNER] Need help destroying vehicles - Evocator - 30.03.2014

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


Re: [BEGINNER] Need help destroying vehicles - dovys11 - 30.03.2014

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

/thread