Raised This Month: $ Target: $400
 0% 

[Solved]Random doesn't work


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
#8 SickneSS
BANNED
Join Date: Sep 2008
Location: Here
Old 07-10-2010 , 07:51   [Solved]Random doesn't work
Reply With Quote #1

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)
        }
    } 
#8 SickneSS is offline
Send a message via MSN to #8 SickneSS Send a message via Skype™ to #8 SickneSS
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 07-10-2010 , 08:31   Re: Random doesn't work
Reply With Quote #2

cs_set_user_team(random_num(1,Player),cs_get_ user_team(Index))
Backstabnoob is offline
#8 SickneSS
BANNED
Join Date: Sep 2008
Location: Here
Old 07-10-2010 , 08:43   Re: Random doesn't work
Reply With Quote #3

Doesn't present any error,but choice more than 1 player

Last edited by #8 SickneSS; 07-10-2010 at 08:47.
#8 SickneSS is offline
Send a message via MSN to #8 SickneSS Send a message via Skype™ to #8 SickneSS
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
#8 SickneSS
BANNED
Join Date: Sep 2008
Location: Here
Old 07-10-2010 , 09:58   Re: Random doesn't work
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
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
#8 SickneSS is offline
Send a message via MSN to #8 SickneSS Send a message via Skype™ to #8 SickneSS
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-10-2010 , 10:06   Re: Random doesn't work
Reply With Quote #6

I realized this after posting, try the edited code.
__________________
Bugsy is offline
#8 SickneSS
BANNED
Join Date: Sep 2008
Location: Here
Old 07-10-2010 , 10:19   Re: Random doesn't work
Reply With Quote #7

Works,Thanks

Last edited by #8 SickneSS; 07-11-2010 at 07:19.
#8 SickneSS is offline
Send a message via MSN to #8 SickneSS Send a message via Skype™ to #8 SickneSS
Reply



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