SA-MP Forums Archive
[Tutorial] How to make a vehicle locked, and unlock it. - 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] How to make a vehicle locked, and unlock it. (/showthread.php?tid=382659)



How to make a vehicle locked, and unlock it. - Chriham3 - 04.10.2012

Hello. Welcome to my second tutorial.

This time it's how to lock a vehicle for everybody, and unlock it with a /lock command.

This is just a basic code, you may need to script further on it if you want it into your liking.
The lock command will only unlock the vehicle for you.

On top of your script under the defines add this:
pawn Code:
new lockedvehicle[5];
new whatever[MAX_PLAYERS] = 0;
new status[MAX_PLAYERS] = 0;
Then in the Gamemodeinit add this, but make sure you edit it first orelse you will get warnings:

pawn Code:
lockedvehicle[0] = CreateVehicle(VEHICLEID, X, Y, Z, ROTATION, COLOR1, COLOR2, RESPAWNTIME); // defines locked vehicle number 1
    lockedvehicle[1] = CreateVehicle(VEHICLEID, X, Y, Z, ROTATION, COLOR1, COLOR2, RESPAWNTIME); // defines locked vehicle number 2
    lockedvehicle[2] = CreateVehicle(VEHICLEID, X, Y, Z, ROTATION, COLOR1, COLOR2, RESPAWNTIME); // defines locked vehicle number 3
    lockedvehicle[3] = CreateVehicle(VEHICLEID, X, Y, Z, ROTATION, COLOR1, COLOR2, RESPAWNTIME); // defines locked vehicle number 4
    lockedvehicle[4] = CreateVehicle(VEHICLEID, X, Y, Z, ROTATION, COLOR1, COLOR2, RESPAWNTIME); // defines locked vehicle number 5
Add this stock anywhere..
pawn Code:
stock IsALockedVehicle(vehicleid)
{
    for(new i;MAX_VEHICLES>i;i++)
    {
        if(vehicleid == lockedvehicle[i]) return 1;
    }
    return 0;
}
pawn Code:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(ispassenger) return 1;
    if(IsALockedVehicle(vehicleid)) // if is a locked vehicle
    {
        if(whatever[playerid] != 1) // what ever variable you need is not 1
        {
            new Float:x, Float:y, Float:z;
            SendClientMessage(playerid, -1, "You can't enter this vehicle.");
            GetPlayerPos(playerid, x, y, z);
            SetPlayerPos(playerid, x, y, z+0.5);
        }
    }
    return 1;
}
Now, whenever you try to enter that vehicle it will deny you and say it is locked.
To unlock it, make some sort or command or any other function to unlock it for only that player id with
pawn Code:
whatever[playerid] = 1;
and lock it for that player again with
pawn Code:
whatever[playerid] = 0;
Example on OnPlayerCommandText

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp("/lock", cmdtext, true, 5) == 0)
    {
        if(status[playerid] == 0)
        {
            whatever[playerid] = 1;
            status[playerid] = 1;
            SendClientMessage(playerid, -1, "Unlocked.");
            return 1;
        }
        if(status[playerid] == 1)
        {
            whatever[playerid] = 0;
            status[playerid] = 0;
            SendClientMessage(playerid, -1, "Locked.");
            return 1;
        }
        return 1;
    }
    return 0;
}
The outcome is:

[ame]http://www.youtube.com/watch?v=YSe4l6ORlaM[/ame]


Re: How to make a vehicle locked, and unlock it. - ObieHZG - 04.10.2012

Thanks for the help.


Re: How to make a vehicle locked, and unlock it. - xMCx - 04.10.2012

nice ,but you could of explain it in a better way..


Re: How to make a vehicle locked, and unlock it. - CoDeZ - 04.10.2012

To be honest i found this better than using SetVehicleParamsEx
Thanks.


Re: How to make a vehicle locked, and unlock it. - Benzke - 05.10.2012

Nice tutorial, I like it, but instead of using
pawn Code:
GetPlayerPos(playerid, x, y, z);
SetPlayerPos(playerid, x, y, z+0.5);
We can also use
pawn Code:
ClearAnimations(playerid);
Looks more better.


Re: How to make a vehicle locked, and unlock it. - TuFF12 - 08.11.2015

But what if player lock one car and get another one. Will the locket car get unlock?

Sorry for my bad english!