timer wont stop
#1

I made this code:
Код:
new timer1;
Код:
	if (strcmp("/rainbow", cmdtext, true, 10) == 0)
  {
  new aa=0,bb;
  aa++;
  bb = aa%2;
	if(bb == 0)
	{
 	KillTimer(timer1);
	}
  timer1 = SetTimerEx("rainbow",50,1,"i",playerid);
  return 1;
	}
Код:
forward rainbow(playerid);
public rainbow(playerid)
{
	new ab, ac;
 	ab=random(127);
 	ac=random(127);
 	new veh;
	veh = GetPlayerVehicleID(playerid);
	ChangeVehicleColor(veh, ab, ac);
}
This code just randomly changes a cars colors. But the problem is that I can't make it stop. I also made a "rainbowoff" cmd which only had "killtimer(timer1)", but it still didn't want to stop. Please help
Reply
#2

When you kill the timer, the next line of the code will always restart it. Try giving your IF statement an ELSE part, and move the SetTimerEx line into there.

Код:
if(bb == 0)
{
  KillTimer(timer1);
}else{
  timer1 = SetTimerEx("rainbow", 50, 1, "i", playerid);
}
Hope that helps.
Reply
#3

I can't believe i made a silly mistake like that. But the code still doesn't work
I put the timer1 = SetTimerEx("rainbow",50,1,"i",playerid); firstly an else, like you showed and then before the if, but it still doesn't work
Reply
#4

Replace 'new timer1;' with this:
Код:
new timer1[MAX_PLAYERS];
new rainbowing[MAX_PLAYERS];
Then replace the IF statement with:
Код:
if(strcmp(cmd, "/rainbow", true) == 0) 
{
  if(rainbowing[playerid] == 1)
  {
    KillTimer(timer1[playerid]);
    rainbowing[playerid] = 0;
  }else{
    timer1[playerid] = SetTimerEx("rainbow", 50, 1, "i", playerid);
    rainbowing[playerid] = 1;
  }
  return 1;
}
This has changed it so every player has a timer variable of their own. Each player also has a variable that checks to see if their vehicle is currently "rainbowing" or not. If you do not have MAX_PLAYERS defined, change it to the amount of players allowed in the server at the same time. This might not be the most efficient way of doing it but it might get it working. *fingers crossed*

EDIT: oh and feel free to change the variable name "rainbowing", couldn't think of a more appropriate one
Reply
#5

pawn Код:
new IsRainbowing[MAX_PLAYERS];
pawn Код:
if(strcmp("/rainbow", cmdtext, true) == 0)
{
    if(IsRainbowing[playerid])
        KillTimer((IsRainbowing[playerid] - 1)), IsRainbowing[playerid] = false;
    else    IsRainbowing[playerid] = SetTimerEx("Reinbowing", 1000, true, "i", playerid) + 1;
    return true;
}
pawn Код:
forward Reinbowing(playerid);
public Reinbowing(playerid)
    if((playerid = GetPlayerVehicleID(playerid)) != INVALID_VEHICLE_ID)
        ChangeVehicleColor(playerid, random(256), random(256));
Reply
#6

Thank you. Btw, the upper script shows that there are 255 car colors in sa. Are there ?
Reply
#7

0 - 126 are all different colors. Many of the colors over 126 are black, but there are other colors. Some examples would be 130, 142, 144, 151... there are loads more.

https://sampwiki.blast.hk/wiki/Colors_List - scroll down to "Other Car Colors" on that page.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)