Cars Never Explode??
#1

Hello samp forum,

I am trying to create a sysem tha prevents a car from exploding when its health is very low. What i have tried to do is create a function thats repeats every half-second and it is meant to get a vehicles damage and if the damage is below 300, set it back to 300 and disable the engine. However this doesnt work for me, i am not sure wether i am scripting it properly but here is the code.

Top of script:
Код:
forward vDamage_Update(playerid, vehicleid);
OnGameModeInit()
Код:
SetTimer("vDamage_Update", 500, 1);
Function at bottom of script:
Код:
public vDamage_Update(playerid, vehicleid)
{
    new engine, lights, alarm, doors, bonnet, boot, objective;
    new Float:vhealth;
    if(IsPlayerInVehicle(playerid, vehicleid))
    {
        GetPlayerVehicleID(playerid);
	if(GetVehicleHealth(vehicleid, vhealth) <300)
	{
	    SetVehicleHealth(vehicleid, 1000);
 	    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
	    SetVehicleParamsEx(vehicleid, 0, 1, alarm, doors, bonnet, boot, objective);
        }
    }
}
I am hoping somebody can tell me why why system doesnt work and maybe help me get it working, I would really appreciate.


Stuoyto
Reply
#2

I think,this should work:
pawn Код:
public vDamage_Update()
{
    for(new playerid; playerid < GetMaxPlayers(); playerid++)
    {
        new engine, lights, alarm, doors, bonnet, boot, objective;
        new Float:vhealth;
        if(IsPlayerInAnyVehicle(playerid))
        {
            GetVehicleHealth(GetPlayerVehicleID(playerid), vhealth);
            if(vhealth <300)
            {
                SetVehicleHealth(GetPlayerVehicleID(playerid), 1000);
                GetVehicleParamsEx(GetPlayerVehicleID(playerid), engine, lights, alarm, doors, bonnet, boot, objective);
                SetVehicleParamsEx(GetPlayerVehicleID(playerid), 0, 1, alarm, doors, bonnet, boot, objective);
            }
        }
    }
}
Reply
#3

You're going to have to create a parameter-less callback then run a loop every run.

pawn Код:
public OnGameModeInit()
{
    SetTimer("MyCallBack",500,1);
}
forward MyCallBack();
public MyCallBack()
{
    new Float:vhp;
    for(new vehicleid; vehicleid<MAX_VEHICLES;vehicleid++)
    {
        if(GetVehicleModel(vehicleid)==0)continue; //only returns 0 when vehicle is invalid (not spawned)
        GetVehicleHealth(vehicleid,vhp);
        if(vhp<300.0)SetVehicleHealth(vehicleid,300);
    }
}
Reply
#4

Ok guys thank you very much i will go and test your methods
Reply
#5

i've editted my post,because there was a mistake in there.
Reply
#6

Thank you very much both of you, i have tested each of you scripts and they worked fine. Dont know how annoying it was for me trying to get that to work. You make it look so simple
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)