AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Pick A Random Player Bug on Start (https://forums.alliedmods.net/showthread.php?t=344908)

Cuttlas 12-19-2023 15:28

Pick A Random Player Bug on Start
 
whenever my plugin is on (amx_pugin 1), it will transfer all players to CT and then pick a random player and transfer him to T. (one vs all CT)

here is the method:

PHP Code:

public PickARandomPlayer()
{
    new 
playersT[32], playersCT[32], numTnumCT
    get_players
(playersT,numT,"e","TERRORIST")
    for(new 
i=0;i<numT;i++)
        
cs_set_user_team(playersT[i],CS_TEAM_CT)

    
get_players(playersCT,numCT,"e","CT")
        
newTerror playersCT[random_num(0,numCT-1)]
    
cs_set_user_team(newTerror ,CS_TEAM_T)


sometimes, when there are only two players on the map and both are T, if I turn on the plugin, it will give this error:

Run time error 10: native error (native "cs_set_user_team")

then I will transfer myself to CT and will re-turn on the plugin again and it will work.

where is the problem and how to avoid such issue?

Natsheh 12-20-2023 02:40

Re: Pick A Random Player Bug on Start
 
Indent your code and add curly brackets to the for loop the compiler is not that smart to determine when the loop ends....

And try again..

georgik57 12-20-2023 07:07

Re: Pick A Random Player Bug on Start
 
If you are guaranteed to have players on both teams, why not just pick a player that's already a t to skip instead of switching him twice?

iceeedr 12-21-2023 00:23

Re: Pick A Random Player Bug on Start
 
Not tested, if I understand correctly that would be it.

Code:

public PickARandomPlayer()
{
        if(getRandomPlayer() != 0)
        {
                new iPlayers[32], iNum, id
                get_players(iPlayers, iNum)
                for(new i = 0; i < iNum; i++)
                {
                        id = iPlayers[i]

                        if(cs_get_user_team(id) == CS_TEAM_CT || cs_get_user_team(id) == CS_TEAM_UNASSIGNED)
                                continue

                        cs_set_user_team(id, CS_TEAM_CT)
                }

                new iTarget = getRandomPlayer()

                if(iTarget)
                {
                        cs_set_user_team(iTarget ,CS_TEAM_T)
                }
        }
}

getRandomPlayer()
{
        new iPlayers[MAX_PLAYERS], iNum
        get_players(iPlayers, iNum, "e", "CT")

        if(!iNum)
                return 0

        return iPlayers[random(iNum)]
}


Cuttlas 12-21-2023 15:13

Re: Pick A Random Player Bug on Start
 
still the same error:

Run time error 10: native error (native "cs_set_user_team")

Bugsy 12-21-2023 18:24

Re: Pick A Random Player Bug on Start
 
PHP Code:


#include <amxmodx>
#include <cstrike>

new const Version[] = "0.1";

enum PlayerData
{
    
SlotID,
    
CsTeams:CSTeam
}

public 
plugin_init() 
{
    
register_plugin"Solo T vs CT" Version "bugsy" );
    
    
register_clcmd"say setteams" "Set_Solo_T_vs_CT" );
}

public 
Set_Solo_T_vs_CTid )
{
    new 
iPlayersMAX_PLAYERS ] , iPlayingPlayersMAX_PLAYERS ][ PlayerData ] , iNum;
    new 
iRandomIndex iPlayer iRandom CsTeams:cstTeam iPlayingPlayersCount;
    
    
//Get all players in the iPlayers array.
    
get_playersiPlayers iNum );
    
    
//Loop through players and load iPlayingPlayers array with those who are on a team
    
for ( new iNum i++ )
    {
        
iPlayer iPlayers];
        
        
//If player is on a team, add to iPlayingPlayers array and increment iPlayingPlayersCount counter
        
if ( CS_TEAM_T <= ( cstTeam cs_get_user_teamiPlayer ) ) <= CS_TEAM_CT )
        {
            
iPlayingPlayersiPlayingPlayersCount ][ SlotID ] = iPlayer;
            
iPlayingPlayersiPlayingPlayersCount ][ CSTeam ] = cstTeam;
            
iPlayingPlayersCount++;
        }
    }
    
    
//We need at least 2 players
    
if ( iPlayingPlayersCount )
    {
        
//Select a random player who will be the solo T
        
iRandomIndex randomiPlayingPlayersCount );
        
iRandom iPlayingPlayersiRandomIndex ][ SlotID ];
        
        
//Move randomly selected solo T to T only if not already a T
        
if ( iPlayingPlayersiRandomIndex ][ CSTeam ] != CS_TEAM_T )
            
cs_set_user_teamiRandom CS_TEAM_T );
        
        
//Loop through remaining players and send them to CT, or do nothing if already CT
        
for ( new iNum i++ )
        {
            
iPlayer iPlayingPlayers][ SlotID ];
            
            
//Only move the player to CT if its not therandom player
            
if ( iPlayer != iRandom )
            {
                
//Only move to CT if not already a CT
                
if ( iPlayingPlayers][ CSTeam ] != CS_TEAM_CT )
                    
cs_set_user_teamiPlayer CS_TEAM_CT );
            }
        }
    }
    else
    {
        
client_printid print_chat "* There is not enough players to set solo T vs all CT." );
    }
    
    return 
PLUGIN_HANDLED;



Cuttlas 12-22-2023 16:29

Re: Pick A Random Player Bug on Start
 
@Bugsy thank you, bro, worked very well :)

but we have a tag mismatch warning on: iPlayingPlayers[ iPlayingPlayersCount ][ CSTeam ] = cstTeam;

Bugsy 12-22-2023 17:07

Re: Pick A Random Player Bug on Start
 
Did you change anything? It compiles clean for me. I would not post code with compile errors or warnings.

Code:

AMX Mod X Compiler 1.9.0.5271
Copyright (c) 1997-2006 ITB CompuPhase
Copyright (c) 2004-2013 AMX Mod X Team

Header size:            368 bytes
Code size:            1360 bytes
Data size:              592 bytes
Stack/heap size:      16384 bytes
Total requirements:  18704 bytes
Done.


Cuttlas 12-23-2023 00:13

Re: Pick A Random Player Bug on Start
 
I'm using amxmodx 1.8.2 bro :)

Bugsy 12-23-2023 00:20

Re: Pick A Random Player Bug on Start
 
Upgrade your server

You can try this:
PHP Code:

iPlayingPlayersiPlayingPlayersCount ][ CSTeam ] = CsTeams:cstTeam

if that doesn't work, try
PHP Code:

enum _:PlayerData
{
    
SlotID,
    
CsTeams:CSTeam




All times are GMT -4. The time now is 10:32.

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