Big Problem -
Bearfist - 25.12.2009
The Problem in this Code is, when 2 players are playin' on my Server , it shows the message "%s Joined the Boxring" twice .. and when 3 player are playin' then it shows that message 3 times ...
Код:
#define BOXSLOTS 300
new PlayersInBoxRing[MAX_PLAYERS];
Код:
for(new i = 0;i < BOXSLOTS; i++)
{
if(IsPlayerConnected(i))
{
if(PlayersInBoxRing[i] == 0)
{
PlayersInBoxRing[i]++;
format(string,sizeof(string),"%s joined the Boxring!",name);
SendClientMessageToAll(COLOR_GREEN,string);
}
else if(PlayersInBoxRing[i] == 1)
{
PlayersInBoxRing[i]++;
format(string,sizeof(string),"%s joined the Boxring as Challenger!",name);
SendClientMessageToAll(COLOR_GREEN,string);
}
else if(PlayersInBoxRing[i] <= 2)
{
SendClientMessage(playerid,COLOR_RED,"Their are already 2 Guys Boxing!");
return 1;
}
}
}
Please help me
Re: Big Problem -
DeathOnaStick - 25.12.2009
I dont understand for what you need the "i"... Please tell me what PlayersInBoxRing actually means and when the whole code is called.
Re: Big Problem -
Babul - 25.12.2009
yes. you are first looping through the boxslots. i presume there are more than 1, so the check for being online is done twice.
due to the fact the player is checked twice, and his boxslot will match twice too, you only need to swap the first 2 conditions checked:
first "is player connected?", then "check all boxslots":
Код:
if(IsPlayerConnected(i))
{
for(new i = 0;i < BOXSLOTS; i++)
{
if(PlayersInBoxRing[i] == 0)
{
PlayersInBoxRing[i]++;
format(string,sizeof(string),"%s joined the Boxring!",name);
SendClientMessageToAll(COLOR_GREEN,string);
return 1;
}
else if(PlayersInBoxRing[i] == 1)
{
PlayersInBoxRing[i]++;
format(string,sizeof(string),"%s joined the Boxring as Challenger!",name);
SendClientMessageToAll(COLOR_GREEN,string);
return 1;
}
else if(PlayersInBoxRing[i] <= 2)
{
SendClientMessage(playerid,COLOR_RED,"Their are already 2 Guys Boxing!");
return 1;
}
}
}
the variable "i" is assigned to IsPlayerConnected(i) and used in the loop (BOXSLOTS), so the "i" will be resetted to 0 anyways, and should cause a warning like:
warning 219: local variable "i" shadows a variable at a preceding level
...try another variable like "playerid" in IsPlayerConnected()...
Код:
if(IsPlayerConnected(playerid))
{
maybe this helps you fixing the problem too...
you also could print out a small string just for repeat-checking at each loopstart...
Re: Big Problem -
Bearfist - 25.12.2009
alright thanks very much guys =) ..
i changed it to this
Код:
for(new i = 0;i < BOXSLOTS; i++)
{
if(IsPlayerConnected(playerid))
{
if(i == 1)
{
format(string,sizeof(string),"%s joined the Boxring!",name);
SendClientMessageToAll(COLOR_GREEN,string);
return 1;
}
else if(i == 2)
{
format(string,sizeof(string),"%s joined the Boxring as Challenger!",name);
SendClientMessageToAll(COLOR_GREEN,string);
return 1;
}
else if(i <= 3)
{
SendClientMessage(playerid,COLOR_RED,"Their are already 2 Guys Boxing!");
return 1;
}
}
}
That's not the whole code that was my test command
...
but if I type my /boxtest command it always appear
"Their are already 2 Guys Boxing"
.. i thinks it appears because it's a loop but with
I stopped it i think
.. pls Help
Re: Big Problem -
Bearfist - 28.12.2009
Finally i got a Solution =) ...
now i don't use the for- loop ..
I use:
and when I type /box:
It works perfect *happy* ...
thank you =)
LG Bearfist
Re: Big Problem -
Babul - 28.12.2009
ah, nice solution
but beware: your code above may confuse the compiler, maybe while running the script:
its checking first if the condition ==0, then ==1. at the last "else if" you check for <=2, thats done before at the ==0 and ==1, both matched. so the last "else if" gets called alyways.
i suggest you to write "else if (PlayersInBoxRing[i] == 2)". iam sure it was a typo hehe...