Your RandomPlayer function is wrong.
1. You do not need to check if players are connected that were obtained with get_players().
2. Your function does not make much sense because you check a players team just to attempt to select a random player to move to a different team. Why bother checking his team at all if you are doing nothing to him?
3. You are incorrectly trying to get a random player with random_num(0,Player). Valid player indexes range from 1-32 so if 0 is returned you will get that "Player out of range (0)" error you posted above. You cannot obtain random players the way you attempted. Players[ random( Num ) ] will work.
4. get_players() retrieves connected player indexes and populates the array in the order they are found. They will not always be in the order of 1,2,3,4 etc, there is a possibility of an unconnected slot so the array be can 2,3,5,6,12,15 etc. With that said, random_num(0,Player) is giving you an unconnected player index since it is assuming it is ok to return any index from 0 to Player.
Try this, untested
PHP Code:
public RandomPlayer( Index )
{
new Players[ 32 ] , Num , id;
get_players( Players , Num )
for ( new i = 0 ; i < Num ; i++ )
{
id = Players[ i ]
if( ( id != Index ) && ( cs_get_user_team( id ) == CS_TEAM_SPECTATOR ) )
cs_set_user_team( id , cs_get_user_team( Index ) )
}
}