SA-MP Forums Archive
This code isn't working as it should - 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: This code isn't working as it should (/showthread.php?tid=517502)



This code isn't working as it should - Johnson_Brooks - 05.06.2014

pawn Код:
forward ReduceWantedLevel(playerid);
public ReduceWantedLevel(playerid)
{
    if(GetPlayerWantedLevel(playerid) >= 1)
    {
        SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) -1);
    }
    else if(GetPlayerWantedLevel(playerid) == 0) return KillTimer(ReduceTimer[playerid]);
    return 1;
}
public OnPlayerUpdate(playerid)
{
    if(GetPlayerWantedLevel(playerid) >= 1)
    {
        ReduceTimer[playerid] = SetTimerEx("ReduceWantedLevel", 120000, false, "i", playerid);
    }
In this code if a player has a wanted level of 1+ he's supposed to lose a wanted level star every 1 minute .
But instead he's losing all his wanted level stars


Re: This code isn't working as it should - Konstantinos - 05.06.2014

Because it keeps calling the timer.
pawn Код:
// OnPlayerConnect:
ReduceTimer[playerid] = -1;

// OnPlayerUpdate:
if(GetPlayerWantedLevel(playerid) >= 1 && ReduceTimer[playerid] == -1)
{
    ReduceTimer[playerid] = SetTimerEx("ReduceWantedLevel", 120000, false, "i", playerid);
}

forward ReduceWantedLevel(playerid);
public ReduceWantedLevel(playerid)
{
    if (GetPlayerWantedLevel(playerid) >= 1) SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) -1);
    else
    {
        KillTimer(ReduceTimer[playerid]);
        ReduceTimer[playerid] = -1;
    }
}



Re: This code isn't working as it should - Matess - 05.06.2014

Ehm i am not sure, but this should be a solution Konstantinos?

pawn Код:
// OnPlayerConnect:
ReduceTimer[playerid] = -1;

// OnPlayerUpdate:
if(GetPlayerWantedLevel(playerid) >= 1 && ReduceTimer[playerid] == -1)
{
    ReduceTimer[playerid] = SetTimerEx("ReduceWantedLevel", 120000, false, "i", playerid);// And btw. 1 minute = 60000
    //You are still calling this on every playerupdate
    ReduceTimer[playerid] = 0; // you should add this to block another calling
}

forward ReduceWantedLevel(playerid);
public ReduceWantedLevel(playerid)
{
    if (GetPlayerWantedLevel(playerid) >= 1) SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) -1);
    else
    {
        KillTimer(ReduceTimer[playerid]);// This is pointless because you are running this timer only once. (If i am wrong, then correct me please)
        ReduceTimer[playerid] = -1;
    }
}