Looping
#1

Hello,

I'm using this code to check is the player near any Stadium entrance. It should return "successful" when the player is near and "not successful" when the player is away.
Код:
for (new i; i < sizeof(Stadium); i++)
{
     if(IsPlayerInRangeOfPoint(playerid, 2.5, StadiumEntrance[i][x], StadiumEntrance[i][y], StadiumEntrance[i][z]))
     SendClientMessage(playerid, -1, "successful");
     else SendClientMessage(playerid, -1, "not successful");
     break;
}
The problem is that everytime it returns "not successful", independently the player is near or not.

EDIT: This problem is solved.
Reply
#2

Remove the else sendclientmessage blabla "not succesfull" line.

Sorry cant copy paste the codings. Im on my phone.
Reply
#3

I know that I will work fine without it, but I want to keep this line.
Reply
#4

Quote:
Originally Posted by Darkwood17
Посмотреть сообщение
I know that I will work fine without it, but I want to keep this line.
Then I'll leave this open for anyone who can code it. Im home in 2 hours, I might have some time to do it then if none responded by then.
Reply
#5

You can't possibly know if it's been unsuccessful before having gone through the entire loop, therefore that message should be outside and after the loop. You will need to set a variable in case it was successful, otherwise the message will always be sent, regardless.
Reply
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
You can't possibly know if it's been unsuccessful before having gone through the entire loop, therefore that message should be outside and after the loop. You will need to set a variable in case it was successful, otherwise the message will always be sent, regardless.
Thanks it works now.
Reply
#7

Sorry for bumping. I'll post here, instead of making new topic.
I used Vince's method and the problem above is fixed now, but another problem occurs.
I'm using this code (this is a command):
Код:
for (new i; i < sizeof(Stadium); i++)
{
     if (IsPlayerInRangeOfPoint(playerid, 2.5, StadiumEntrance[i][x], StadiumEntrance[i][y], StadiumEntrance[i][z]))
     APlayerData[playerid][NearStadium] = true;
}
if (APlayerData[playerid][NearStadium] == true) SendMessage(playerid, "you're near the stadium");
else SendMessage(playerid, "you're away from the stadium");
NearStadium is in the player data enum, and it's false by default (when player connect).
When I enter the server (not near the stadium) and type the command, it return "you're away from the stadium", which is right.
Then when I'm standing near the stadium and enter the command it return "you're near the stadium", which is right too.
But after that, when I go away from the stadium, it always return "you're near the stadium", which is wrong.
Reply
#8

PHP код:
for (new isizeof(Stadium); i++) {
     if(
IsPlayerInRangeOfPoint(playerid2.5StadiumEntrance[i][x], StadiumEntrance[i][y], StadiumEntrance[i][z])) {
        
SendClientMessage(playerid, -1"successful");
                return 
1;
    }
} else {
    
SendClientMessage(playerid, -1"not successful");
    return 
1;

You realize...
You're calling the ELSE statement every single time right?
So lets say you have...
10 Inside your array..
You're going to get 10 messages...
The code Posted above should work.
Reply
#9

Quote:
Originally Posted by Prokill911
Посмотреть сообщение
PHP код:
for (new isizeof(Stadium); i++) {
     if(
IsPlayerInRangeOfPoint(playerid2.5StadiumEntrance[i][x], StadiumEntrance[i][y], StadiumEntrance[i][z])) {
        
SendClientMessage(playerid, -1"successful");
                return 
1;
    }
} else {
    
SendClientMessage(playerid, -1"not successful");
    return 
1;

You realize...
You're calling the ELSE statement every single time right?
So lets say you have...
10 Inside your array..
You're going to get 10 messages...
The code Posted above should work.
Maybe you're talking about the first problem which is already solved (thanks to Vince).
The problem wasn't spamming with messages. There's break; in the code, which will prevent the spam.

The problem now is this:
Quote:
Originally Posted by Darkwood17
Посмотреть сообщение
I'm using this code (this is a command):
Код:
for (new i; i < sizeof(Stadium); i++)
{
     if (IsPlayerInRangeOfPoint(playerid, 2.5, StadiumEntrance[i][x], StadiumEntrance[i][y], StadiumEntrance[i][z]))
     APlayerData[playerid][NearStadium] = true;
}
if (APlayerData[playerid][NearStadium] == true) SendMessage(playerid, "you're near the stadium");
else SendMessage(playerid, "you're away from the stadium");
NearStadium is in the player data enum, and it's false by default (when player connect).
When I enter the server (not near the stadium) and type the command, it return "you're away from the stadium", which is right.
Then when I'm standing near the stadium and enter the command it return "you're near the stadium", which is right too.
But after that, when I go away from the stadium, it always return "you're near the stadium", which is wrong.
Reply
#10

Looking at that..
You need to be literally stood directly on the X Y Z Or just off of it..

You're allowing 2.5 which is literally NOT A big distance at all.
So unless you're stood like directly on it...
It wont trigger the
Код:
APlayerData[playerid][NearStadium] = true;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)