[Tutorial] Locking Vehicles - Custom Way
#1

Since I had some troubles with "SetVehicleParamsForPlayer" or other SetVehicleParams function I decided to make my own easy-to-do locking system, which works perfectly so far, this is my first tutorial, don't be rude :P please tell me if I have forgot something

if you want to test if it works, just change this line
pawn Код:
if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid)
to this one
pawn Код:
if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] == vehicleid)
after that just lock vehicle, go out and try to enter

Global Variables
First of all we have to create global variables that will store our data for every player and vehicle (every player can own only one vehicle) and text variable for TextDraw, 2 more variables we will be using later and function to hide TextDraw

pawn Код:
new PlayerCar[MAX_PLAYERS], LockedCar[1000];
new Text:Locked;
new MsgLocked[MAX_PLAYERS], LockedTimer[MAX_PLAYERS];
forward RemoveLock(playerid);
change length of LockedCar to amount of cars you are using or just some high number to be sure you can lock any car

We have to set PlayerCar[playerid] to -1 when player connects, this will tell our server that this player don't have any locked cars
pawn Код:
public OnPlayerConnect(playerid)
{
    PlayerCar[playerid] = -1;
    return 1;
}
Commands
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/lock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle."); // player has to be in a vehicle
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver."); // player has to be a driver
        if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
        return 1;
    }
    if(!strcmp(cmdtext, "/unlock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle.");
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver.");
        PlayerCar[playerid] = -1; // tells server that this player don't have any locked vehicle
        LockedCar[GetPlayerVehicleID(playerid)] = 0; // unlocks current vehicle
        return 1;
    }
    return 0;
}
pretty much self-explanatory

now for the main part;

Checking vehicle Lock
pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid) // if player is trying to enter locked vehicle and he is not bound to that vehicle
    {
        if(MsgLocked[playerid] == 1) // if there's already message showing for player
        {
            TextDrawHideForPlayer(playerid, Locked); // hides message
            MsgLocked[playerid] = 0; // there's no more any message regarding locked car
            KillTimer(LockedTimer[playerid]); // kills timer
        }
        TogglePlayerControllable(playerid, true); // stops player from entering vehicle (it's not his vehicle, why should he enter it?)
        Locked = TextDrawCreate(10.0, 250.0, "This Vehicle is Locked"); // pretty much self-explanatory
        TextDrawUseBox(Locked, 1);
        TextDrawBoxColor(Locked, 0x00000055);
        TextDrawTextSize(Locked, 125, 5);
        TextDrawShowForPlayer(playerid, Locked); // shows TextDraw to player
        LockedTimer[playerid] = SetTimer("RemoveLock", 4000, false); // sets timer for hiding TextDraw
        MsgLocked[playerid] = 1; // we have TextDraw showing
    }
    return 1;
}

public RemoveLock(playerid)
{
    if(MsgLocked[playerid] == 1) // if there's message showing for player
    {
        TextDrawHideForPlayer(playerid, Locked); // hides message
        MsgLocked[playerid] = 0; // there's no more any message regarding locked car
    }
}

public OnVehicleSpawn(vehicleid)
{
    LockedCar[vehicleid] = 0; // when vehicle has respawned (nobody has entered it for a while) server will unlock it
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerCar[i] == vehicleid)
            PlayerCar[i] = -1; // unbounds player from this vehicle so anyone can enter it
    }
    return 1;
}
so our Lock system looks like this:

Final Version

pawn Код:
new PlayerCar[MAX_PLAYERS], LockedCar[1000];
new Text:Locked;
new MsgLocked[MAX_PLAYERS], LockedTimer[MAX_PLAYERS];
forward RemoveLock(playerid);

public OnPlayerConnect(playerid)
{
    PlayerCar[playerid] = -1;
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/lock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle."); // player has to be in a vehicle
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver."); // player has to be a driver
        if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
        return 1;
    }
    if(!strcmp(cmdtext, "/unlock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle.");
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver.");
        PlayerCar[playerid] = -1; // tells server that this player don't have any locked vehicle
        LockedCar[GetPlayerVehicleID(playerid)] = 0; // unlocks current vehicle
        return 1;
    }
    return 0;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid) // if player is trying to enter locked vehicle and he is not bound to that vehicle
    {
        if(MsgLocked[playerid] == 1) // if there's already message showing for player
        {
            TextDrawHideForPlayer(playerid, Locked); // hides message
            MsgLocked[playerid] = 0; // there's no more any message regarding locked car
            KillTimer(LockedTimer[playerid]); // kills timer
        }
        TogglePlayerControllable(playerid, true); // stops player from entering vehicle (it's not his vehicle, why should he enter it?)
        Locked = TextDrawCreate(10.0, 250.0, "This Vehicle is Locked"); // pretty much self-explanatory
        TextDrawUseBox(Locked, 1);
        TextDrawBoxColor(Locked, 0x00000055);
        TextDrawTextSize(Locked, 125, 5);
        TextDrawShowForPlayer(playerid, Locked); // shows TextDraw to player
        LockedTimer[playerid] = SetTimer("RemoveLock", 4000, false); // sets timer for hiding TextDraw
        MsgLocked[playerid] = 1; // we have TextDraw showing
    }
    return 1;
}

public RemoveLock(playerid)
{
    if(MsgLocked[playerid] == 1) // if there's message showing for player
    {
        TextDrawHideForPlayer(playerid, Locked); // hides message
        MsgLocked[playerid] = 0; // there's no more any message regarding locked car
    }
}

public OnVehicleSpawn(vehicleid)
{
    LockedCar[vehicleid] = 0; // when vehicle has respawned (nobody has entered it for a while) server will unlock it
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerCar[i] == vehicleid)
            PlayerCar[i] = -1; // unbounds player from this vehicle so anyone can enter it
    }
    return 1;
}
Reply
#2

Thanks mate i ama try this one
Reply
#3

see:

F:\test.pwn(4) : error 017: undefined symbol "LockedCar"
F:\test.pwn(4) : warning 215: expression has no effect
F:\test.pwn(4) : error 001: expected token: ";", but found "]"
F:\test.pwn(4) : error 029: invalid expression, assumed zero
F:\test.pwn(4) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


4 Errors.
Reply
#4

Quote:
Originally Posted by Tuntun
Посмотреть сообщение
see:

F:\test.pwn(4) : error 017: undefined symbol "LockedCar"
F:\test.pwn(4) : warning 215: expression has no effect
F:\test.pwn(4) : error 001: expected token: ";", but found "]"
F:\test.pwn(4) : error 029: invalid expression, assumed zero
F:\test.pwn(4) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


4 Errors.
post me your fourth line, or better everything you made :P but from I see I guess you didn't declare global variable LockedCar[1000], or you have some letter uppercase/lowercase
Reply
#5

Код:
#include <a_samp>
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid) 
    {
        if(MsgLocked[playerid] == 1) 
        {
            TextDrawHideForPlayer(playerid, Locked); 
            MsgLocked[playerid] = 0;
            KillTimer(LockedTimer[playerid]); 
        }
        TogglePlayerControllable(playerid, true); 
        Locked = TextDrawCreate(10.0, 250.0, "This Vehicle is Locked"); 
        TextDrawUseBox(Locked, 1);
        TextDrawBoxColor(Locked, 0x00000055);
        TextDrawTextSize(Locked, 125, 5);
        TextDrawShowForPlayer(playerid, Locked); /
        LockedTimer[playerid] = SetTimer("RemoveLock", 4000, false);
        MsgLocked[playerid] = 1; 
    }
    return 1;
}

public RemoveLock(playerid)
{
    if(MsgLocked[playerid] == 1)
    {
        TextDrawHideForPlayer(playerid, Locked); 
        MsgLocked[playerid] = 0; 
    }
}

public OnVehicleSpawn(vehicleid)
{
    LockedCar[vehicleid] = 0; 
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerCar[i] == vehicleid)
            PlayerCar[i] = -1; 
    }
    return 0;
}
Reply
#6

Quote:
Originally Posted by Tuntun
Посмотреть сообщение
Код:
#include <a_samp>
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid) 
    {
        if(MsgLocked[playerid] == 1) 
        {
            TextDrawHideForPlayer(playerid, Locked); 
            MsgLocked[playerid] = 0;
            KillTimer(LockedTimer[playerid]); 
        }
        TogglePlayerControllable(playerid, true); 
        Locked = TextDrawCreate(10.0, 250.0, "This Vehicle is Locked"); 
        TextDrawUseBox(Locked, 1);
        TextDrawBoxColor(Locked, 0x00000055);
        TextDrawTextSize(Locked, 125, 5);
        TextDrawShowForPlayer(playerid, Locked); /
        LockedTimer[playerid] = SetTimer("RemoveLock", 4000, false);
        MsgLocked[playerid] = 1; 
    }
    return 1;
}

public RemoveLock(playerid)
{
    if(MsgLocked[playerid] == 1)
    {
        TextDrawHideForPlayer(playerid, Locked); 
        MsgLocked[playerid] = 0; 
    }
}

public OnVehicleSpawn(vehicleid)
{
    LockedCar[vehicleid] = 0; 
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerCar[i] == vehicleid)
            PlayerCar[i] = -1; 
    }
    return 0;
}
ofc, you didn't declare global variable or commands, this is not whole code, next time read whole tutorial :P I'll update it with whole code at the end, give me 2 min
Reply
#7

Can you fix it?
Reply
#8

Try this: if((LockedCar[vehicleid] == 1) && (PlayerCar[playerid] != vehicleid))
Reply
#9

Quote:
Originally Posted by Tuntun
Посмотреть сообщение
Can you fix it?
I already posted whole source code to the first post

Quote:
Originally Posted by SomebodyAndMe
Посмотреть сообщение
Try this: if((LockedCar[vehicleid] == 1) && (PlayerCar[playerid] != vehicleid))
he just copied half of the script, this won't help
Reply
#10

You can also use ClearAnimations to prevent player from entering a vehicle

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if (vehicleid == some_id)
    {
        SendClientMessage(playerid, -1, "GTFO");
        ClearAnimations(playerid);
    }
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)