The problem with the code is the loop is badly written.
The only reason it would let anyone in the server is if the SteamIds array contains one or more of the same SteamId.
Let me explain further.
Let's say you have these steamids stored in that array:
STEAM_0:0:1234567
STEAM_0:0:2345678
STEAM_0:0:4567890
Now, the player with the id STEAM_0:0:2345678 joins.
At the first run of the loop it would compare the steamid. Since STEAM_0:0:2345678 is not equal to STEAM_0:0:1234567 ( first in loop ) it kicks immediately.
Now let's say the person who's first in the array joins.
The first run will be fine because STEAM_0:0:1234567 does equal to STEAM_0:0:1234567.
The problem is the loop doesn't know this is a point to break, so it would continue to the second steamid.
And because STEAM_0:0:12345678 does not equal to STEAM_0:0:2345678 the person will then be kicked even though the first run of the loop was a success.
Basically you have to do something like this:
In the first two examples you can continue adding code after the loop for both statements (if or if not the player was found)
The first example is using a variable called match_found. This will add readability and make it easier for people to understand. You, of course, are using one more variable, but that's not really any problem.
Code:
new szID[35], match_found;
get_user_authid(client, szID, charsmax(szID))
for ( new i = 0 ; i < sizeof SteamIds ; i++ ) {
if ( equali(szID, SteamIds[i]) ) {
match_found = true;
break;
}
}
if ( ! match_found )
server_cmd("kick #%d", get_user_userid(client))
The second example can be a bit more confusing for beginners.
This is how it works:
The loop increments after every
finished loop.
It will run as long as the statement (i < sizeof SteamIds) is true.
If there's a match, the variable i will always be lower than sizeof SteamIds because we break the function
before it increments the variable i.
If there's no match it will run the loop, incrementing the variable i until it no longer matches condition. I.e. the variable i is equal to or larger than sizeof SteamIds. Because it's incrementing in steps of 1, it will always equal to sizeof SteamIds.
Code:
new szID[35], i;
get_user_authid(client, szID, charsmax(szID))
for ( i = 0 ; i < sizeof SteamIds ; i++ ) {
if ( equali(szID, SteamIds[i]) )
break;
}
if ( i == sizeof SteamIds )
server_cmd("kick #%d", get_user_userid(client))
The third differs that it will return upon match, making all the code under the loop unreachable for players found in the array of SteamIds.
Code:
new szID[35];
get_user_authid(client, szID, charsmax(szID))
for ( new i = 0 ; i < sizeof SteamIds ; i++ ) {
if ( equali(szID, SteamIds[i]) )
return
}
server_cmd("kick #%d", get_user_userid(client))
So which one should you use? The two first is all about coding style and personal preference. The third option is the most simple one, not requiring any variables or double-checking. But it has the least amount of flexibility. It depends on if you want to do anything to the actual matching players.
If you want to learn more, here's how a for loop works (more or less):
Code:
#include <amxmodx>
public plugin_init() {
register_plugin("Test Plugin 6", "", "");
new i = 0;
for ( server_print("i is set to %d^n", (i = 0)) ; server_print("Is i(%d) lower than 5? %s", i, i < 5 ? "yes, continue loop." : "no, break loop") && i < 5 ; i++, server_print("i is incremented to %d^n", i) ) {
server_print("Actual contents of the loop, i: %d", i);
}
server_print("Loop is finished, i: %d", i);
}
Code:
i is set to 0
Is i(0) lower than 5? yes, continue loop.
Actual contents of the loop, i: 0
i is incremented to 1
Is i(1) lower than 5? yes, continue loop.
Actual contents of the loop, i: 1
i is incremented to 2
Is i(2) lower than 5? yes, continue loop.
Actual contents of the loop, i: 2
i is incremented to 3
Is i(3) lower than 5? yes, continue loop.
Actual contents of the loop, i: 3
i is incremented to 4
Is i(4) lower than 5? yes, continue loop.
Actual contents of the loop, i: 4
i is incremented to 5
Is i(5) lower than 5? no, break loop
Loop is finished, i: 5
Code:
#include <amxmodx>
public plugin_init() {
register_plugin("Test Plugin 6", "", "");
new i = 0;
for ( server_print("i is set to %d^n", (i = 0)) ; server_print("Is i(%d) lower than 5? %s", i, i < 5 ? "yes, continue loop." : "no, break loop") && i < 5 ; i++, server_print("i is incremented to %d^n", i) ) {
server_print("Actual contents of the loop, i: %d", i);
if ( i == 4 )
break;
}
server_print("Loop is finished, i: %d", i);
}
Code:
i is set to 0
Is i(0) lower than 5? yes, continue loop.
Actual contents of the loop, i: 0
i is incremented to 1
Is i(1) lower than 5? yes, continue loop.
Actual contents of the loop, i: 1
i is incremented to 2
Is i(2) lower than 5? yes, continue loop.
Actual contents of the loop, i: 2
i is incremented to 3
Is i(3) lower than 5? yes, continue loop.
Actual contents of the loop, i: 3
i is incremented to 4
Is i(4) lower than 5? yes, continue loop.
Actual contents of the loop, i: 4
Loop is finished, i: 4
Notice that in both examples the last loop content contains i as 4, but in the second example we break the loop before it increments.
So basically, a for loop constructed like this:
Code:
for ( 0 ; 1 ; 3 ) {
2
}
will be called in that order. 0 will only be called once.
0, 1, 2, 3
1, 2, 3
1, 2, 3
and so further
It's the same as writing a while loop like this:
Code:
0
while ( 1 ) {
2
3
}
__________________