SA-MP Forums Archive
Why does this do this ? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Why does this do this ? (/showthread.php?tid=350243)



Why does this do this ? - Geniuss - 11.06.2012

How comes all my timers do this ?

When ever i hotwire a car it makes everyone wait 10 seconds before they can hotwire a car aging even if they have not done it in hours.. It sends a message to everyone saying you can not hot wire a car again

Please Help

pawn Код:
command(hotwire, playerid, params[])
{
    new engine, lights, alarm, doors, bonnet, boot, objective, string[128];
    new veh = GetPlayerVehicleID(playerid);
    new rand = random(10);
    if(IsPlayerInVehicle(playerid, veh))
    {
        if(AbleToHotWire == 0)
        {
            if(engine == 0)
            {
                switch(rand)
                    {
                        case 1,2,4,7,9:
                        {
                            GetVehicleParamsEx(veh, engine, lights, alarm, doors, bonnet, boot, objective);
                            SetVehicleParamsEx(veh, 1, lights, alarm, doors, bonnet, boot, 0);
                            format(string, sizeof(string), "%s has successfuly hotwired the vehicle", GetNameNoUnderScore(playerid));
                            AbleToHotWire = 1;
                            IsPlayerInHotWiredCar = 1;
                            SetTimer("ResetHotWireTime", 10000, false);
                            NearByMessage(playerid, PURPLE, string);
                        }
                        case 3,5,6,8,10:
                        {
                            format(string, sizeof(string), "%s has failed at hotwiring the vehicle", GetNameNoUnderScore(playerid));
                            NearByMessage(playerid, PURPLE, string);
                            AbleToHotWire = 1;
                            SetTimer("ResetHotWireTime", 10000, false);
                        }
                    }
            }
            if(engine == 1)
            {
                SendClientMessage(playerid, WHITE, "The vehicles engine is on no need to hotwire");
            }
        }
        else
        {
            SendClientMessage(playerid, WHITE, "You can not hotwire a car yet");
        }
    }
}
pawn Код:
forward ResetHotWireTime(playerid);
pawn Код:
public ResetHotWireTime(playerid)
{
    AbleToHotWire = 0;
    SendClientMessage(playerid, WHITE, "You can now hotwire a car again");
}
If you need any more code just ask

Thank You

Please Help


Re: Why does this do this ? - Makaveli93 - 11.06.2012

Try using
pawn Код:
SetTimerEx("ResetHotWireTime", 10000, false, "i", playerid);
instead.


Re: Why does this do this ? - JaTochNietDan - 11.06.2012

You should consider using a function like gettime instead, not to mention your code could do with some cleaning up, here's an example:

pawn Код:
new lastHotwire[MAX_PLAYERS];

command(hotwire, playerid, params[])
{
    if((gettime() - 60) < lastHotwire[playerid]) return SendClientMessage(playerid, WHITE, "You can not hotwire a car yet");
    if(!IsPlayerInAnyVehicle(playerid)) return; // Player is not in any vehicle.
    if(engine == 1) return SendClientMessage(playerid, WHITE, "The vehicles engine is on no need to hotwire"); // One variable for all vehicles?

    if(random(100) > 50)
    {
        // No need to initialize variables unless they are needed
        new engine, lights, alarm, doors, bonnet, boot, objective, string[128];
        new veh = GetPlayerVehicleID(playerid);

        GetVehicleParamsEx(veh, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(veh, 1, lights, alarm, doors, bonnet, boot, 0);
        format(string, sizeof(string), "%s has successfuly hotwired the vehicle", GetNameNoUnderScore(playerid));
        IsPlayerInHotWiredCar = 1; // Single variable for 500 players? Odd!
    }
    else format(string, sizeof(string), "%s has failed at hotwiring the vehicle", GetNameNoUnderScore(playerid));

    lastHotwire[playerid] = gettime();
    NearByMessage(playerid, PURPLE, string);
}
Code is a lot cleaner, easier to read and more efficient. No more useless timers, no more redundant code and/or repeating code!