Timer when player enters vehicle
#1

Hey, I want to add a system which does the following:
When a player enters a vehicle, depending on the vehicle's ID, 20 seconds after the player has entered it shows a message, and after 25 seconds, the vehicle explodes.
I've tried different ways but I have problems using timers s: someone help?
Thanks!
Reply
#2

Here you go,

PHP код:
public OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
    if(
GetVehicleModel(vehicleid) == your id)
    {
         
SetTimerEx("Message"5000false"i"playerid);
    }
    return 
1;
}
forward Message(playerid);
public 
Message(playerid)
{
     
SendClientMessage(playerid, -1"Welcome to the vehicle.");
     
SetTimerEx("Explosion"5000false"i"playerid);
     return 
1;
}
forward Explosion(playerid);
public 
Explosion(playerid)
{
     new 
Float:xFloat:yFloat:z
     GetPlayerPos
(playerid,x,y,z);
     
CreateExplosion(x,y,z,1210.0);
     
SendClientMessage(playerid, -1"Trapped LOL.");
     return 
1;

Hope that helps, Rep if it does lol ^^

KillerDVX,
Reply
#3

@KillerDVX if Player Tries To Enter A Vehicle, "OnPlayerEnterVehicle" Will Call.
so if i Press F and cancel entering car, that vehicle will explode !

its better to use "OnPlayerStateChange" Callback and check player vehicle id after changing state to DRIVER.
also Use SetVehicleHealth To Explode Vehicle
Reply
#4

Or Trying this as well ?

PHP код:
new IsInVeh[MAX_PLAYERS];
public 
OnPlayerEnterVehicle(playeridvehicleidispassenger

    if(
GetVehicleModel(vehicleid) == your id
    { 
         
SetTimerEx("Message"5000false"i"playerid); 
         
IsInVeh[playerid] = 1;
    } 
    return 
1

public 
OnPlayerExitVehicle(playeridvehicleid)
{
        
IsInVeh[playerid] = 0;
    return 
1;
}
forward Message(playerid); 
public 
Message(playerid

     
SendClientMessage(playerid, -1"Welcome to the vehicle."); 
     
SetTimerEx("Explosion"5000false"i"playerid); 
     return 
1

forward Explosion(playerid); 
public 
Explosion(playerid

    if(
IsInVeh[playerid] = 1)

     new 
Float:xFloat:yFloat:
     GetPlayerPos
(playerid,x,y,z); 
     
CreateExplosion(x,y,z,1210.0); 
     
SendClientMessage(playerid, -1"Trapped LOL."); 
 }
     return 
1

Reply
#5

Not working :/ it compiles and all but nothing happens
Reply
#6

Alright so I edited it this way:

Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
	vehid = GetVehicleModel(vehicleid);
	if(vehid == 400 | 405 | 409 | 415 | 418 | 419 | 427 | 440 | 451 | 466)
    {
		SetTimerEx("Message", 20000, false, "i", playerid);
    }
    return 1;
}

forward Message(playerid);
public Message(playerid)
{
	SendClientMessage(playerid, -1, "[BEEP - BEEP - BEEP]");
	SetTimerEx("Explosion", 25000, false, "i", playerid);
	return 1;
}

forward Explosion(playerid);
public Explosion(playerid)
{
	SetVehicleHealth(vehid, 200);
	return 1;
}
It compiles and everything but doesn't work, is there any error? I defined the variable vehid.
Reply
#7

My fault.. x)

Try this one :


PHP код:
new IsInVeh[MAX_PLAYERS]; 
public 
OnPlayerEnterVehicle(playeridvehicleidispassenger)  
{  
    if(
GetVehicleModel(vehicleid) == your id)  
    {  
         
SetTimerEx("Message"5000false"i"playerid);  
         
IsInVeh[playerid] = 1
    }  
    return 
1;  
}  
public 
OnPlayerExitVehicle(playeridvehicleid

        
IsInVeh[playerid] = 0
    return 
1

forward Message(playerid);  
public 
Message(playerid)  
{  
     
SendClientMessage(playerid, -1"Welcome to the vehicle.");  
     
SetTimerEx("Explosion"5000false"i"playerid);  
     return 
1;  
}  
forward Explosion(playerid);  
public 
Explosion(playerid)  
{  
    if(
IsInVeh[playerid] = 1
{  
     new 
pveh =GetPlayerVehicleID(playerid);
     new 
Float:xFloat:yFloat:z  
     GetVehiclePos
(pvehxyz);
     
CreateExplosion(x,y,z,1210.0);  
     
SendClientMessage(playerid, -1"Trapped LOL.");  
 } 
     return 
1;  

Reply
#8

Please DON'T Use OnPlayerEnterVehicle In This case...
i'll Give you an example look at it and learn how i used timers, variables and SAMP functions from Wiki

https://sampwiki.blast.hk/wiki/OnPlayerStateChange
https://sampwiki.blast.hk/wiki/GetVehicleModel
https://sampwiki.blast.hk/wiki/SetVehicleHealth

Код:
#define YOUR_VEHICLE_MODEL X

new ExplodeTimer[MAX_PLAYERS];
new bool:IsPlayerInExplodeVehicle[MAX_PLAYERS];

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER)
    {
        new VehicleID = GetPlayerVehicleID(playerid);
		if(GetVehicleModel(VehicleID) == YOUR_VEHICLE_MODEL)
		{
			IsPlayerInExplodeVehicle[playerid] = true;
			ExplodeTimer[playerid] = SetTimerEx("ShowExplodeMessage", 20000, false, "i", playerid);
		}
    }
	if(oldstate == PLAYER_STATE_DRIVER && IsPlayerInExplodeVehicle[playerid] == true)
	{
		IsPlayerInExplodeVehicle[playerid] = false;
		KillTimer(ExplodeTimer[playerid]);
	}
    return 1;
}

forward ShowExplodeMessage(playerid);
public ShowExplodeMessage(playerid)
{
	if(IsPlayerInExplodeVehicle[playerid] == true)
	{
		SendClientMessage(playerid, -1, "Your Message Here");
		ExplodeTimer[playerid] = SetTimerEx("ExplodeVehicle", 5000, false, "i", playerid);
	}
	return 1;
}

forward ExplodeVehicle(playerid);
public ExplodeVehicle(playerid)
{
	if(IsPlayerInExplodeVehicle[playerid] == true)
	{
		new pVehicleID = GetPlayerVehicleID(playerid)
		SetVehicleHealth(pVehicleID, 0.0);
		IsPlayerInExplodeVehicle[playerid] = false;
	}
	return 1;
}
change X in this line: "#define YOUR_VEHICLE_MODEL X" to your vehicle model

good luck
Reply
#9

Quote:
Originally Posted by M4D
Посмотреть сообщение
Please DON'T Use OnPlayerEnterVehicle In This case...
i'll Give you an example look at it and learn how i used timers, variables and SAMP functions from Wiki

https://sampwiki.blast.hk/wiki/OnPlayerStateChange
https://sampwiki.blast.hk/wiki/GetVehicleModel
https://sampwiki.blast.hk/wiki/SetVehicleHealth

Код:
#define YOUR_VEHICLE_MODEL X

new ExplodeTimer[MAX_PLAYERS];
new bool:IsPlayerInExplodeVehicle[MAX_PLAYERS];

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_DRIVER)
    {
        new VehicleID = GetPlayerVehicleID(playerid);
		if(GetVehicleModel(VehicleID) == YOUR_VEHICLE_MODEL)
		{
			IsPlayerInExplodeVehicle[playerid] = true;
			ExplodeTimer[playerid] = SetTimerEx("ShowExplodeMessage", 20000, false, "i", playerid);
		}
    }
	if(oldstate == PLAYER_STATE_DRIVER && IsPlayerInExplodeVehicle[playerid] == true)
	{
		IsPlayerInExplodeVehicle[playerid] = false;
		KillTimer(ExplodeTimer[playerid]);
	}
    return 1;
}

forward ShowExplodeMessage(playerid);
public ShowExplodeMessage(playerid)
{
	if(IsPlayerInExplodeVehicle[playerid] == true)
	{
		SendClientMessage(playerid, -1, "Your Message Here");
		ExplodeTimer[playerid] = SetTimerEx("ExplodeVehicle", 5000, false, "i", playerid);
	}
	return 1;
}

forward ExplodeVehicle(playerid);
public ExplodeVehicle(playerid)
{
	if(IsPlayerInExplodeVehicle[playerid] == true)
	{
		new pVehicleID = GetPlayerVehicleID(playerid)
		SetVehicleHealth(pVehicleID, 0.0);
		IsPlayerInExplodeVehicle[playerid] = false;
	}
	return 1;
}
change X in this line: "#define YOUR_VEHICLE_MODEL X" to your vehicle model

good luck
Thanks! I only get this error when I compile:

error 001: expected token: ";", but found "-identifier-"
Reply
#10

Quote:
Originally Posted by aalbero99
Посмотреть сообщение
Thanks! I only get this error when I compile:

error 001: expected token: ";", but found "-identifier-"
Edit: works perfectly! Thanks!!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)