 |
|
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
|

07-10-2010
, 09:53
Re: Random doesn't work
|
#4
|
Your RandomPlayer function is wrong. Backstabnoob please do not try to help people if you don't know what you're doing yourself.
- You do not need to check if players are connected that were obtained with get_players() if you are manipulating the players immediately after being obtained.
- Your function does not make much sense because you loop through all players retrieved with get_players and check each players team just to attempt to select a random player to move to a different team if the checked player is a spectator. Why bother checking his team at all if you are doing nothing with him?
- 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 that you posted above. You cannot obtain random players the way you attempted. Players[ random( Num ) ] or Players[ random_num( 0 , Num-1 ) ] will work, though. Notice the random player index is being obtained from the array that was populated with get_players, not directly.
- 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, there is a possibility that random_num(0,Player) will give you an unconnected player index since it will return all numbers in the range of 0 to Player.
Try this, untested
PHP Code:
public RandomPlayer( Index ) { new Players[ 32 ] , Num , id; new bool:Checked[ 32 ] , NumChecked; get_players( Players , Num ); while ( NumChecked < Num ) { id = Players[ random( Num ) ]; if( !Checked[ id ] ) { if ( ( id != Index ) && ( cs_get_user_team( id ) == CS_TEAM_SPECTATOR ) ) { cs_set_user_team( id , cs_get_user_team( Index ) ) break; } else { Checked[ id ] = true; NumChecked++; } } } }
__________________
Last edited by Bugsy; 07-10-2010 at 10:21.
|
|
|
|