AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Solved]Random doesn't work (https://forums.alliedmods.net/showthread.php?t=131925)

#8 SickneSS 07-10-2010 07:51

[Solved]Random doesn't work
 
Debug...
Code:

L 07/10/2010 - 08:47:20: [CSTRIKE] Player out of range (0)
L 07/10/2010 - 08:47:20: [AMXX] Displaying debug trace (plugin "Untitled.amxx")
L 07/10/2010 - 08:47:20: [AMXX] Run time error 10: native error (native "cs_set_user_team")
L 07/10/2010 - 08:47:20: [AMXX] [0] Untitled.sma::RandomPlayer (line 284)
L 07/10/2010 - 08:47:20: [AMXX] [1] Untitled.sma::MenuWinnerHandler (line 165)

Lines...

PHP Code:

public RandomPlayer(Index) {
    
    new 
Players[32]
    new 
Num
    get_players
(Players,Num)
    
    for(new 
0;<= Num;i++)
    {
        new 
Player Players[i]
        
        if(
is_user_connected(Player) && cs_get_user_team(Player) == CS_TEAM_SPECTATOR)
            
cs_set_user_team(random_num(0,Player),cs_get_user_team(Index))
    }


PHP Code:

    if(item == MENU_EXIT)
    {
        if(
Choosing[Looser])
            
ChatColor(id,"!y[!gMix Maker!y] No puedes elegir en este momento")
        else if(
Choosing[Winner])
        {
            
RandomPlayer(Winner)
            
ChatColor(0,"!y[!gMix Maker!y] %s hizo una eleccion random",name)
        }
    } 


Backstabnoob 07-10-2010 08:31

Re: Random doesn't work
 
cs_set_user_team(random_num(1,Player),cs_get_ user_team(Index))

#8 SickneSS 07-10-2010 08:43

Re: Random doesn't work
 
Doesn't present any error,but choice more than 1 player

Bugsy 07-10-2010 09:53

Re: Random doesn't work
 
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++;
            }
        }
    }



#8 SickneSS 07-10-2010 09:58

Re: Random doesn't work
 
Quote:

Originally Posted by Bugsy (Post 1234235)
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 RandomPlayerIndex 
{
    new 
Players32 ] , Num id;
    
    
get_playersPlayers Num )
        
    for ( new 
Num i++ )
    {
        
id Players]
            
        if( ( 
id != Index ) && ( cs_get_user_teamid ) == CS_TEAM_SPECTATOR ) )
            
cs_set_user_teamid cs_get_user_teamIndex ) )
    }



Ok,thanks but a question before testing,where you get a random player? id = all players

Bugsy 07-10-2010 10:06

Re: Random doesn't work
 
I realized this after posting, try the edited code.

#8 SickneSS 07-10-2010 10:19

Re: Random doesn't work
 
Works,Thanks :)


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

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