PDA

View Full Version : GetMaxClients won't work


anakin_cstrike
01-02-2012, 12:56
So i have this for instance


#include <sourcemod>

new gi_MaxPlayers;

public OnPluginStart()
{
gi_MaxPlayers = GetMaxClients ( );
}

public Action:MyFunc ( /* ... */ )
{
MyStock ( );
return;
}

stock MyStock ( /* ... */ )
{
for ( new i_Index = 1; i_Index <= gi_MaxPlayers; i_Index++ )
{
if ( ! IsClientInGame ( i_Index ) || ! IsPlayerAlive ( i_Index ) )
continue;

PrintToChat ( i_Index,"test" );
}
}

I'm the only one connected to the server and it doesn't prints me that message, nor i receive some error in the server console. What's the problem? thanks

EDIT: ok so i kinda fixed it, i think the problem was that i have maxclients set to 16, i just put 32 or random number instead of the index and it works...but why?

EDIT2: Now i'm getting this error and it bothers me for a while
L 01/02/2012 - 20:09:02: [SM] Native "IsClientInGame" reported: Client index 17 is invalid
L 01/02/2012 - 20:09:02: [SM] Displaying call stack trace for plugin "who.smx":
L 01/02/2012 - 20:09:02: [SM] [0] Line 79, C:\sourcemod\scripting\who.sp::HookBulletImpa ct()

the code is like this

for ( new i_Player = 1; i_Player <= 32; i_Player++ )
{
if ( ! ( IsClientInGame ( i_Player ) && ! IsPlayerAlive ( i_Player ) ) )
continue;
// stuff...

pheadxdll
01-02-2012, 13:28
for(new i=1; i<=MaxClients; i++)
{
if(IsClientInGame(i) && IsPlayerAlive(i))
{
PrintToChat(i, "Test");
}
}


Loop through your clients like this. MaxClients is a predefined variable at runtime.
Doing this "i_Player <= 32;" was probably your problem, IsClientInGame likes to throw error when you give it invalid client indexes. MaxClients is the limit you want.

Bacardi
01-02-2012, 13:34
Why you need do this, there is already MaxClients.
public const MaxClients; /**< Maximum number of players the server supports (dynamic) */http://docs.sourcemod.net/api/index.php?fastload=file&id=25&file=&

lokizito
01-02-2012, 14:02
Also, MaxClients and GetMaxClients() shouldn't be called before OnMapStart(), so, shouldn't be called at OnPluginStart().

anakin_cstrike
01-02-2012, 14:57
works, thanks...kinda new to source scripting :D