Help please on Killing a Timer, it won't die!
#1

For some reason I can't get the timer to Kill using /detectoroff after it's created with /detectoron. How do I fix this so It kills it?

pawn Код:
new DetectorTimer[MAX_PLAYERS];
    new DetectorOn[MAX_PLAYERS]=1;
    if (strcmp("/detectoron", cmdtext, true, 11) == 0)
    {
        if(PlayerInfo[playerid][pDetector])
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                if(DetectorTimer[playerid] != -1) KillTimer(DetectorTimer[playerid]);//
                DetectorTimer[playerid] = SetTimerEx("RunDetector", 2000, true, "i", playerid);
                SendClientMessage(playerid,-1,"Detector On");
                DetectorOn[playerid]=1;
                return 1;
            }
            SendClientMessage(playerid, COLOR_GREY, "** You aren't in a vehicle!");
            return 1;
        }
        SendClientMessage(playerid, COLOR_GREY, "** You don't have a Radar Detector!");
        return 1;
    }
    if (strcmp("/detectoroff", cmdtext, true, 12) == 0)
    {
        if(PlayerInfo[playerid][pDetector])
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                if(DetectorOn[playerid]==0)return SendClientMessage(playerid,-1,"Detector Already OFF");
                SendClientMessage(playerid,-1,"Detector OFF");
                KillTimer(DetectorTimer[playerid]);
                DetectorOn[playerid]=0;
                return 1;
            }
            SendClientMessage(playerid, COLOR_GREY, "** You aren't in a vehicle!");
            return 1;
        }
        SendClientMessage(playerid, COLOR_GREY, "** You don't have a Radar Detector!");
        return 1;
    }
**Note that I will also need to add the kill timer function to other places in my GM**
Reply
#2

Can you explain some more what is the problem and show us your RunDetector code
Reply
#3

Maybe it's because you don't check if the timer is already active? So if you do /detectoron, 2 times you will have 2 timers but can only kill one of them.
Reply
#4

I am getting the message "Detector OFF" so I know that it's proceding correctly...


Run Detector Code:
pawn Код:
public RunDetector(playerid)
{
    new counter = 0, string[128];
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerInRangeOfPoint(i, 30.0, x, y, z) && i != playerid && IsPlayerConnected(i) && IsACop(i))
        {
            TextDrawShowForPlayer(i, Textdraw79[playerid]);
            // ignore this format(string, sizeof(string), "%s (%d)", Nick(i), i);
            counter++;
        }
    }
    if(counter == 0) return TextDrawShowForPlayer(playerid, Textdraw78[playerid]);
    return 1;
}
By the way this was happening when the text draws were ClientMessages (so the text draws aren't the problem...)
Reply
#5

Try this, it's your code but simplified, leave no chance of 2 timers on same var. It toggles it on/off one command.

pawn Код:
if (strcmp("/detector", cmdtext, true, 11) == 0)
{
    if(PlayerInfo[playerid][pDetector])
    {
        if(IsPlayerInAnyVehicle(playerid))
        {
            if( !DetectorOn[playerid] )
            {
                DetectorTimer[playerid] = SetTimerEx("RunDetector", 2000, true, "i", playerid);
                SendClientMessage(playerid,-1,"Detector On");
                DetectorOn[playerid]=1;
                return 1;
            }
            else
            {
                SendClientMessage(playerid,-1,"Detector OFF");
                KillTimer(DetectorTimer[playerid]);
                DetectorOn[playerid]=0;
                return 1;
            }
        }
        SendClientMessage(playerid, COLOR_GREY, "** You aren't in a vehicle!");
        return 1;
    }
}
Reply
#6

I will test this in a bit Thanks, so KillTimer(DetectorTimer[playerid]); will work if I add it to other places in my GM right?
Reply
#7

Yes, if it's global.
Reply
#8

Global? What would need to be global exactly?

You just mean: forward RunDetector(playerid) ?
Reply
#9

I'm a bit confused, I don't see what you mean? It looks fine. Why exactly won't it turn on though?

Thanks: jakejohnsonusa
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)