AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Choosing a random player (https://forums.alliedmods.net/showthread.php?t=347270)

tedaimlocks 04-15-2024 16:01

Choosing a random player
 
Can someone help with the code below?I dont know how to get a random player and give them the stuff

HTML Code:

#include <amxmodx>
#include <cstrike>
#include <fun>
#include <colorchat>

#define PLUGIN "Random Hero"
#define VERSION "1.0"
#define AUTHOR "tedaimlocks"

public plugin_init() {
        register_plugin(PLUGIN, VERSION, AUTHOR);
        register_logevent("logevent_RoundStart", 2, "1=Round_Start");
}

public logevent_RoundStart()
        set_task(1.0, "ChooseHero");


public ChooseHero()
{
        // Choose a random player every round and give them the stuff under

        set_user_armor(id, get_user_armor(id) + 150);
        set_user_health(id, get_user_health(id) + 150);
        give_item(id, "weapon_hegrenade");
        give_item(id, "weapon_flashbang");

        new szName[32];
        get_user_name(id, szName, charsmax(szName));
        ColorChat(0, GREEN, "^x01[^x04SPK ZM^x01]^x04 %s ^x03was chosen to be the ^x04Hero ^x03of the round! They have got ^x04+150HP+150AP+MULTIJUMPS", szName);
}


bigdaddy424 04-15-2024 17:06

Re: Choosing a random player
 
you can get a random player like this
Code:

new id, players = get_playersnum()
if (players)
        id = random(1, players)


mlibre 04-15-2024 17:12

Re: Choosing a random player
 
Quote:

Originally Posted by tedaimlocks (Post 2820993)
Can someone help with the code below?I dont know how to get a random player and give them the stuff

Spoiler

Code:
public ChooseHero() {     // Choose a random player every round and give them the stuff under         new players[32], num; get_players(players, num, "a")         new id = players[random(num)]         set_user_armor(id, get_user_armor(id) + 150)     set_user_health(id, get_user_health(id) + 150)         give_item(id, "weapon_hegrenade")     give_item(id, "weapon_flashbang")         new szName[32]; get_user_name(id, szName, charsmax(szName))         ColorChat(0, GREEN, "^x01[^x04SPK ZM^x01]^x04 %s ^x03was chosen to be the ^x04Hero ^x03of the round! They have got ^x04+150HP+150AP+MULTIJUMPS", szName) }

WATCH_D0GS UNITED 04-15-2024 18:48

Re: Choosing a random player
 
random(num)

-> random_num(1, num)

reason: invalid player 0.

mlibre 04-15-2024 19:38

Re: Choosing a random player
 
Quote:

Originally Posted by WATCH_D0GS UNITED (Post 2820997)
random(num)

-> random_num(1, num)

reason: invalid player 0.

:nono: in get_players does not apply, in get_maxplayers yes!

Quote:

Originally Posted by TheKidz (Post 1331005)
PHP Code:

new g_MaxPlayers;
public 
plugin_init() {
 
g_MaxPlayers get_maxplayers()
}

public 
func() {
 for( new 
1<= get_maxplayersi++ ) {
  
//do stuff
 
}


PHP Code:

public func() {
 new 
Clients32 ], iNum
 get_players
ClientsiNum )
 for( new 
0iNumi++ ) {
  
//do stuff
 
}




WATCH_D0GS UNITED 04-15-2024 19:50

Re: Choosing a random player
 
Based on what its said, it doesn't matter:
----------------------------------

native random(max);

Description

Returns a random number between 0 and a specified upper bound.

----------------------------------

tedaimlocks 04-16-2024 02:34

Re: Choosing a random player
 
Quote:

Originally Posted by mlibre (Post 2820996)
Code:
public ChooseHero() {     // Choose a random player every round and give them the stuff under         new players[32], num; get_players(players, num, "a")         new id = players[random(num)]         set_user_armor(id, get_user_armor(id) + 150)     set_user_health(id, get_user_health(id) + 150)         give_item(id, "weapon_hegrenade")     give_item(id, "weapon_flashbang")         new szName[32]; get_user_name(id, szName, charsmax(szName))         ColorChat(0, GREEN, "^x01[^x04SPK ZM^x01]^x04 %s ^x03was chosen to be the ^x04Hero ^x03of the round! They have got ^x04+150HP+150AP+MULTIJUMPS", szName) }

It gives errors if no player is connected ( i think )

run time error 4 : index out of bounds

mlibre 04-16-2024 08:17

Re: Choosing a random player
 
Quote:

Originally Posted by WATCH_D0GS UNITED (Post 2821004)
Based on what its said, it doesn't matter:
----------------------------------

native random(max);

Description

Returns a random number between 0 and a specified upper bound.

----------------------------------

so if there are 3 players, let's say, you can throw a larger random number like 4 you mean

Quote:

Originally Posted by tedaimlocks (Post 2821008)
...run time error 4 : index out of bounds

test

Code:
public ChooseHero() {     // Choose a random player every round and give them the stuff under         new players[32], num; get_players(players, num, "a")         if( !num )     {         return     }         //... }

Bugsy 04-16-2024 10:24

Re: Choosing a random player
 
PHP Code:

new iPlayers32 ] , iNum iRandom;
get_playersiPlayers iNum );

if ( 
iNum )
{
      
iRandom iPlayers randomiNum ) ];



WATCH_D0GS UNITED 04-16-2024 18:48

Re: Choosing a random player
 
random() will select a random value from 0 to the specified max value.

random(7) -> {0,1,2,3,4,5,6,7}

which sometimes, will lead to this:

PHP Code:

iRandom iPlayers 

Valid client entity indexes are from 1 to 32.

As an additional note, there are two more issues which are related to the use of get_players() native which we will not point out here,
but we are pretty sure that if someone knows how entity index slots works in the game this person will understand why.


All times are GMT -4. The time now is 15:28.

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