Unlock car when driver leaves.
#1

Can anyone assist me with unlocking the car doors when the driver leaves? Getting a bit confused here..

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        //Now what? Lol
    }
    return 1;
}
How I lock:
pawn Код:
YCMD:vlock(playerid, params[], help)
{
    new string[128];
    new vid = GetPlayerVehicleID(playerid);
    new State = GetPlayerState(playerid);
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xFFFFFFAA, "You have to be inside a vehicle to lock the doors right?");
    if(State != PLAYER_STATE_DRIVER) return SendClientMessage(playerid, 0xFFFFFFAA, "You must be the driver of the vehicle to lock it.");
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(i != playerid)
        {
            SetVehicleParamsForPlayer(vid, i, 0, 1);
        }
    }
    GetPlayerName(playerid, sendername, sizeof(sendername));
    format(string, sizeof(string), "* %s locked his/her car doors.", sendername);
    SendClientMessage(playerid, 0xFFFF00AA, "Vehicle locked!");
    new Float:pX, Float:pY, Float:pZ;
    GetPlayerPos(playerid,pX,pY,pZ);
    PlayerPlaySound(playerid,1056,pX,pY,pZ);
    return 1;
}
How I unlock:
pawn Код:
YCMD:vunlock(playerid, params[], help)
{
    new string[128];
    new vid = GetPlayerVehicleID(playerid);
    new State = GetPlayerState(playerid);
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xFFFFFFAA, "You have to be inside a vehicle to unlock the doors right?");
    if(State != PLAYER_STATE_DRIVER) return SendClientMessage(playerid, 0xFFFFFFAA, "You must be the driver of the vehicle to unlock it.");
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(i != playerid)
        {
            SetVehicleParamsForPlayer(vid, i, 0, 0);
        }
    }
    GetPlayerName(playerid, sendername, sizeof(sendername));
    format(string, sizeof(string), "* %s unlocked his/her car doors.", sendername);
    SendClientMessage(playerid, 0xFFFF00AA, "Vehicle unlocked!");
    new Float:pX, Float:pY, Float:pZ;
    GetPlayerPos(playerid,pX,pY,pZ);
    PlayerPlaySound(playerid,1057,pX,pY,pZ);
    return 1;
}
Getting the vehicle params' the hard part for me.. I'm confused...
Reply
#2

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        new vid = GetPlayerVehicleID(playerid);
        for(new i = 0; i < MAX_PLAYERS; i++)
        {
            SetVehicleParamsForPlayer(vid, i, 0, 0);
        }
    }
    return 1;
}
Reply
#3

There is a easier way:

PHP код:
public OnPlayerExitVehicle(playeridvehicleid)
{
        new 
string[128], sendername[MAX_PLAYER_NAME];
        
GetPlayerName(playeridsendernamesizeof(sendername));
        
SetVehicleParamsForPlayer(vehicleidplayerid00);
        
SendClientMessage(playerid, -1"Doors unlocked");
        
GameTextForPlayer(playerid,"~r~Doors unlocked"40003);
         
format(stringsizeof(string), "* %s unlocked his/her car doors."sendername);
         
SendClientMessageToAll(-string);
         return 
1;

Reply
#4

That would work... if he wanted the doors to unlock for passengers as well...
You would need to save the vehicle id to a player variable so when they exit, you can check the last vehicle they were in, and change its parameters.

Something like:
pawn Код:
new lastvehicle[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    lastvehicle[playerid] = -1; //They do not have a last vehicle so -1 / invalid
    return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(newstate == PLAYER_STATE_ONFOOT && oldstate == PLAYER_STATE_DRIVER)
    {
        if(lastvehicle[playerid] != -1)
        {
            new engine, lights, alarm, doors, bonnet, boot, objective;
            GetVehicleParamsEx(lastvehicle[playerid], engine, lights, alarm, doors, bonnet, boot, objective);
            SetVehicleParamsEx(lastvehicle[playerid], engine, lights, alarm, 1, bonnet, boot, objective);
        }
    }
    if(newstate == PLAYER_STATE_DRIVER && oldstate == PLAYER_STATE_ONFOOT)
    {
        lastvehicle[playerid] = GetPlayerVehicleID(playerid);
    }
    return 1;
}
Reply
#5

I used Mineralo's method along with Benzo's Last Vehicle and got it working. Thanks guys.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)