SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 11/06/2019, 02:48 PM   #1
binnyl
Big Clucker
 
Join Date: Aug 2017
Posts: 178
Reputation: 2
Default Detect when player leaves train

I have to detect when player leaves a train, i've tried it with no sucess
Code:
public OnPlayerExitVehicle(playerid, vehicleid) {
    // resolver bug sair train
    if (GetVehicleModel(vehicleid) == 537) {
        SetCameraBehindPlayer(playerid);
        // tirar CP
        DisablePlayerCheckpointEx(playerid);
        // destruir veiculo
        DestroyVehicle(vehicleid);
        VehicleInfo[vehicleid][vDonoID] = 0;
        VehicleInfo[vehicleid][vType] = 0;
        if(tremRota[playerid] < 5) tremRota[playerid] = 0;
    }
}
I think its something because train cant be done by CreateVehicle, have to be done by AddStaticVehicleEx
Code:
    if(VehicleInfo[vehid][vModel] == 537) VehicleInfo[vehid][vID] = AddStaticVehicleEx(VehicleInfo[vehid][vModel],VehicleInfo[vehid][vParkX], VehicleInfo[vehid][vParkY], VehicleInfo[vehid][vParkZ], VehicleInfo[vehid][vParkR], cor1,cor2,0);
    else VehicleInfo[vehid][vID] = CreateVehicle(VehicleInfo[vehid][vModel], VehicleInfo[vehid][vParkX], VehicleInfo[vehid][vParkY], VehicleInfo[vehid][vParkZ], VehicleInfo[vehid][vParkR], cor1,cor2,0);
Is anyone know the way?
binnyl is offline   Reply With Quote
Old 11/06/2019, 05:04 PM   #2
NoteND
Big Clucker
 
Join Date: May 2016
Posts: 145
Reputation: 3
Default Re: Detect when player leaves train

Try using same method but with OnPlayerStateChange

https://wiki.sa-mp.com/wiki/OnPlayerStateChange

PHP Code:
public OnPlayerStateChange(playeridnewstateoldstate)
{
    if(
oldstate == PLAYER_STATE_DRIVER && newstate == ONFOOT)
    {
        new 
vehicleid GetPlayerVehicleID(playerid);
        ...
    }
    return 
1;

NoteND is offline   Reply With Quote
Old 11/06/2019, 05:36 PM   #3
binnyl
Big Clucker
 
Join Date: Aug 2017
Posts: 178
Reputation: 2
Default Re: Detect when player leaves train

Quote:
Originally Posted by NoteND View Post
Try using same method but with OnPlayerStateChange

https://wiki.sa-mp.com/wiki/OnPlayerStateChange

PHP Code:
public OnPlayerStateChange(playeridnewstateoldstate)
{
    if(
oldstate == PLAYER_STATE_DRIVER && newstate == ONFOOT)
    {
        new 
vehicleid GetPlayerVehicleID(playerid);
        ...
    }
    return 
1;

Not work ;(
binnyl is offline   Reply With Quote
Old 11/06/2019, 05:52 PM   #4
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,625
Reputation: 535
Default Re: Detect when player leaves train

Quote:
Originally Posted by binnyl View Post
Not work ;(
When leaving a train (or any vehicle) it will always call OnPlayerStateChange. It will either change from Driver to On Foot, or from Passenger to On Foot. So depending on if the player is the driver or passenger you need to check it accordingly.

Otherwise use OnPlayerKeyStateChange and check for the enter/exit key.

The problem with OnPlayerExitVehicle is, if you are in one of the carriages, it will call the callback with the VehicleID of the carriage sometimes, which is technically invalid (you'll always have to use the VehicleID of the train itself).
NaS is offline   Reply With Quote
Old 13/06/2019, 06:04 PM   #5
binnyl
Big Clucker
 
Join Date: Aug 2017
Posts: 178
Reputation: 2
Default Re: Detect when player leaves train

Quote:
Originally Posted by NaS View Post
When leaving a train (or any vehicle) it will always call OnPlayerStateChange. It will either change from Driver to On Foot, or from Passenger to On Foot. So depending on if the player is the driver or passenger you need to check it accordingly.

Otherwise use OnPlayerKeyStateChange and check for the enter/exit key.

The problem with OnPlayerExitVehicle is, if you are in one of the carriages, it will call the callback with the VehicleID of the carriage sometimes, which is technically invalid (you'll always have to use the VehicleID of the train itself).
But when i've set:
PHP Code:
    // sair do trem
    
if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        new 
vehicleid GetPlayerVehicleID(playerid); 
        if (
GetVehicleModel(vehicleid) == 537) {
            
printf("leaves train");
            
SetCameraBehindPlayer(playerid);
            
// tirar CP
            
DisablePlayerCheckpointEx(playerid);
            
// destruir veiculo
            
DestroyVehicle(vehicleid);
            
VehicleInfo[vehicleid][vDonoID] = 0;
            
VehicleInfo[vehicleid][vType] = 0;
            if(
tremRota[playerid] < 5tremRota[playerid] = 0;
        }
    } 
It's not printing anything on server_log.txt
binnyl is offline   Reply With Quote
Old 14/06/2019, 09:36 PM   #6
SiNaGaMeR
Little Clucker
 
Join Date: Feb 2018
Location: Iran
Posts: 49
Reputation: 0
Default Re: Detect when player leaves train

Use the following codes:
PHP Code:
forward OnPlayerUpdate(playerid); 
PHP Code:
new sina[MAX_PLAYERS];
sina[playerid] = 0
PHP Code:
public OnPlayerUpdate(playerid)
{
new 
VehID GetPlayerVehicleID(playerid);  
if (
GetVehicleModel(VehID) == 537) { 
sina[playerid] = 1;
return 
1;
}
return 
1;

PHP Code:
    // sair do trem 
    
if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT
    { 
            if(
sina[playerid] == 1)
            {
            
printf("leaves train"); 
            
SetCameraBehindPlayer(playerid); 
            
// tirar CP 
            
DisablePlayerCheckpointEx(playerid); 
            
// destruir veiculo 
            
DestroyVehicle(vehicleid); 
            
VehicleInfo[vehicleid][vDonoID] = 0
            
VehicleInfo[vehicleid][vType] = 0
            if(
tremRota[playerid] < 5tremRota[playerid] = 0
            
sina[playerid] = 0;
            }
    } 
__________________
...</> Sᴄʀɪᴘᴛɪɴɢ... </>...

─═हई ★ SɪƝαƓaʍε尺 ★ ईह═─


SiNaGaMeR is offline   Reply With Quote
Old 19/06/2019, 02:08 PM   #7
pedrotvr
Huge Clucker
 
Join Date: Nov 2013
Posts: 346
Reputation: 7
Default Re: Detect when player leaves train

Quote:
Originally Posted by SiNaGaMeR View Post
Use the following codes:
PHP Code:
forward OnPlayerUpdate(playerid); 
PHP Code:
new sina[MAX_PLAYERS];
sina[playerid] = 0
PHP Code:
public OnPlayerUpdate(playerid)
{
new 
VehID GetPlayerVehicleID(playerid);  
if (
GetVehicleModel(VehID) == 537) { 
sina[playerid] = 1;
return 
1;
}
return 
1;

PHP Code:
    // sair do trem 
    
if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT
    { 
            if(
sina[playerid] == 1)
            {
            
printf("leaves train"); 
            
SetCameraBehindPlayer(playerid); 
            
// tirar CP 
            
DisablePlayerCheckpointEx(playerid); 
            
// destruir veiculo 
            
DestroyVehicle(vehicleid); 
            
VehicleInfo[vehicleid][vDonoID] = 0
            
VehicleInfo[vehicleid][vType] = 0
            if(
tremRota[playerid] < 5tremRota[playerid] = 0
            
sina[playerid] = 0;
            }
    } 

It wont work
pedrotvr is offline   Reply With Quote
Old 21/06/2019, 01:49 PM   #8
binnyl
Big Clucker
 
Join Date: Aug 2017
Posts: 178
Reputation: 2
Default Re: Detect when player leaves train

bump
binnyl is offline   Reply With Quote
Old 21/06/2019, 02:12 PM   #9
RewardK
Little Clucker
 
Join Date: Aug 2018
Location: Romania
Posts: 3
Reputation: 0
Default Re: Detect when player leaves train

The only method is to start a timer when the player enter in train and to check every second if is in a train.
When the player is not anymore in train, stop the timer and execute that code.

If you don't know how to do this, PM me.
RewardK is offline   Reply With Quote
Old 25/06/2019, 10:54 AM   #10
Mugala
Gangsta
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 773
Reputation: 34
Default Re: Detect when player leaves train

returned vehicle ID is 0 in that case, when oldstate is driver or passenger it means that player is already on foot.
you have to make something, like if(newstate == PLAYER_STATE_EXIT_VEHICLE && oldstate == PLAYER_STATE_PASSENGER)
__________________
here to help
Mugala 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 TextDraw selection ? gandzia Scripting Help 3 20/01/2014 04:06 AM
Cars destroying when player leaves fordawinzz Scripting Help 6 24/09/2012 06:56 PM
When a player leaves a checkpoint Vero Help Archive 2 05/07/2011 07:12 PM
If player leaves the area The Woody Help Archive 6 13/04/2011 07:51 AM
Destroy car when player leaves it Scenario Help Archive 6 16/10/2010 02:24 PM


All times are GMT. The time now is 06:33 AM.


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