SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 26/10/2019, 06:35 AM   #1
binnyl
Huge Clucker
 
Join Date: Aug 2017
Posts: 258
Reputation: 6
Default How to detect when player leaves the train

I'm trying to every time player leaves the train
The train destroy
But i couldn't detect when player leaves the train


Isn't this the way?
Code:
public OnPlayerStateChange(playerid, newstate, oldstate) {
    if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        new vehicleid = GetPlayerVehicleID(playerid); 
        if (GetVehicleModel(vehicleid) == 537) {
            DestroyVehicle(vehicleid);
            SendClientMessageToAll(-1, "leaves train");
            printf("leaves train");
            SetCameraBehindPlayer(playerid);
        }
    }
It is not printing a shit

I'm creating the train on this:
VehicleInfo[vehid][vID] = AddStaticVehicleEx(VehicleInfo[vehid][vModel],VehicleInfo[vehid][vParkX], VehicleInfo[vehid][vParkY], VehicleInfo[vehid][vParkZ], VehicleInfo[vehid][vParkR], cor1,cor2,0);
binnyl is offline   Reply With Quote
Old 26/10/2019, 09:35 AM   #2
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 958
Reputation: 167
Default Re: How to detect when player leaves the train

GetPlayerVehicleID returns 0 because the player is no longer in a vehicle. Create a global array and set it to vehicle ID when the player enters a train. When the state from driver becomes on foot, check if the variable is not 0 (so in train), reset it and execute your code.
Calisthenics is offline   Reply With Quote
Old 26/10/2019, 10:26 AM   #3
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,516
Reputation: 1635
Default Re: How to detect when player leaves the train

Or,
https://wiki.sa-mp.com/wiki/OnPlayerExitVehicle.
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Kwarde is online now   Reply With Quote
Old 28/10/2019, 05:18 AM   #4
binnyl
Huge Clucker
 
Join Date: Aug 2017
Posts: 258
Reputation: 6
Default Re: How to detect when player leaves the train

Quote:
Originally Posted by Calisthenics View Post
GetPlayerVehicleID returns 0 because the player is no longer in a vehicle. Create a global array and set it to vehicle ID when the player enters a train. When the state from driver becomes on foot, check if the variable is not 0 (so in train), reset it and execute your code.
ty to answer, but i dont got how to do it, srry

Quote:
Originally Posted by Kwarde View Post
Code:
public OnPlayerExitVehicle(playerid, vehicleid) {
if(GetVehicleModel(vehicleid) == 537)
    {
        DestroyVehicle(vehicleid);
        SendClientMessageToAll(-1, "leaves train");
        printf("leaves train");
        SetCameraBehindPlayer(playerid);
        // tirar CP
        DisablePlayerCheckpointEx(playerid);
        // destruir veiculo
        DestroyVehicle(vehicleid);
        VehicleInfo[vehicleid][vDonoID] = 0;
        VehicleInfo[vehicleid][vType] = 0;
        if(tremRota[playerid] < 6) tremRota[playerid] = 0;
    }
}
also dont work
binnyl is offline   Reply With Quote
Old 28/10/2019, 07:51 AM   #5
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,161
Reputation: 73
Default Re: How to detect when player leaves the train

callback OnPlayerExitVehicle is not called when you're leaving a train, your first variation is correct, use OnPlayerStateChange but save vehicle's ID at OnPlayerEnterVehicle, later you will use that ID in OnPlayerStateChange to detect if its a train or not.
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 28/10/2019, 08:29 AM   #6
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 958
Reputation: 167
Default Re: How to detect when player leaves the train

Quote:
Originally Posted by Kwarde View Post
Spamming exit key will probably trigger the callback and it is not guaranteed that the player will exit from vehicle when in high speed.

Quote:
Originally Posted by binnyl View Post
ty to answer, but i dont got how to do it, srry
pawn Code:
// global per-player array
new Player_TrainVehicleID[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    Player_TrainVehicleID[playerid] = 0; // reset to 0
    return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if (oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER)
    {
        new vehicleid = GetPlayerVehicleID(playerid);

        if (GetVehicleModel(vehicleid) == 537) // if player is now driving a train
        {
            Player_TrainVehicleID[playerid] = vehicleid; // store the train vehicle ID
        }
    }
    else if (oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        if (Player_TrainVehicleID[playerid] != 0) // player was in train and now they are on-foot
        {
            DestroyVehicle(Player_TrainVehicleID[playerid]); // destroy the train
            Player_TrainVehicleID[playerid] = 0; // reset again to avoid destroying the wrong vehicle
            SetCameraBehindPlayer(playerid);

            // debug messages
            SendClientMessageToAll(-1, "leaves train");
            printf("leaves train");
        }
    }
    return 1;
}
Calisthenics is offline   Reply With Quote
Old 28/10/2019, 04:31 PM   #7
binnyl
Huge Clucker
 
Join Date: Aug 2017
Posts: 258
Reputation: 6
Default Re: How to detect when player leaves the train

Quote:
Originally Posted by Calisthenics View Post
Spamming exit key will probably trigger the callback and it is not guaranteed that the player will exit from vehicle when in high speed.


pawn Code:
// global per-player array
new Player_TrainVehicleID[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    Player_TrainVehicleID[playerid] = 0; // reset to 0
    return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if (oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER)
    {
        new vehicleid = GetPlayerVehicleID(playerid);

        if (GetVehicleModel(vehicleid) == 537) // if player is now driving a train
        {
            Player_TrainVehicleID[playerid] = vehicleid; // store the train vehicle ID
        }
    }
    else if (oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        if (Player_TrainVehicleID[playerid] != 0) // player was in train and now they are on-foot
        {
            DestroyVehicle(Player_TrainVehicleID[playerid]); // destroy the train
            Player_TrainVehicleID[playerid] = 0; // reset again to avoid destroying the wrong vehicle
            SetCameraBehindPlayer(playerid);

            // debug messages
            SendClientMessageToAll(-1, "leaves train");
            printf("leaves train");
        }
    }
    return 1;
}
THANK YOU
+REP
binnyl is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect when player leaves train binnyl Scripting Help 9 25/06/2019 11:54 AM
Detect when player leaves TextDraw selection ? gandzia Scripting Help 3 20/01/2014 05:06 AM
When a player leaves a checkpoint Vero Help Archive 2 05/07/2011 08:12 PM
If player leaves the area The Woody Help Archive 6 13/04/2011 08:51 AM
Destroy car when player leaves it Scenario Help Archive 6 16/10/2010 03:24 PM


All times are GMT. The time now is 07:54 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.