Raised This Month: $ Target: $400
 0% 

[Solved]Random doesn't work


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-10-2010 , 09:53   Re: Random doesn't work
Reply With Quote #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 RandomPlayerIndex 
{
    new 
Players32 ] , Num id;
    new 
bool:Checked32 ] , NumChecked;
    
    
get_playersPlayers Num );
        
    while ( 
NumChecked Num )
    {
        
id PlayersrandomNum ) ];
            
        if( !
Checkedid ] )
        {
            if ( ( 
id != Index ) && ( cs_get_user_teamid ) == CS_TEAM_SPECTATOR ) )
            {
                
cs_set_user_teamid cs_get_user_teamIndex ) )
                break;
            }
            else
            {
                
Checkedid ] = true;
                
NumChecked++;
            }
        }
    }

__________________

Last edited by Bugsy; 07-10-2010 at 10:21.
Bugsy is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:14.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode