Raised This Month: $51 Target: $400
 12% 

Choosing a random player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
tedaimlocks
Member
Join Date: Jan 2024
Old 04-15-2024 , 16:01   Choosing a random player
Reply With Quote #1

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);
}

Last edited by tedaimlocks; 04-17-2024 at 03:40.
tedaimlocks is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 04-15-2024 , 17:06   Re: Choosing a random player
Reply With Quote #2

you can get a random player like this
Code:
new id, players = get_playersnum()
if (players)
	id = random(1, players)
__________________
bigdaddy424 is offline
mlibre
Veteran Member
Join Date: Nov 2015
Location: return PLUGIN_CONTINUE
Old 04-15-2024 , 17:12   Re: Choosing a random player
Reply With Quote #3

Quote:
Originally Posted by tedaimlocks View Post
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) }
__________________
mlibre is offline
WATCH_D0GS UNITED
Senior Member
Join Date: Jan 2023
Old 04-15-2024 , 18:48   Re: Choosing a random player
Reply With Quote #4

random(num)

-> random_num(1, num)

reason: invalid player 0.
__________________
💻Know Our New Blog👄
🔗tube2downs.blogspot.com
WATCH_D0GS UNITED is offline
mlibre
Veteran Member
Join Date: Nov 2015
Location: return PLUGIN_CONTINUE
Old 04-15-2024 , 19:38   Re: Choosing a random player
Reply With Quote #5

Quote:
Originally Posted by WATCH_D0GS UNITED View Post
random(num)

-> random_num(1, num)

reason: invalid player 0.
in get_players does not apply, in get_maxplayers yes!

Quote:
Originally Posted by TheKidz View Post
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
 
}

__________________
mlibre is offline
WATCH_D0GS UNITED
Senior Member
Join Date: Jan 2023
Old 04-15-2024 , 19:50   Re: Choosing a random player
Reply With Quote #6

Based on what its said, it doesn't matter:
----------------------------------

native random(max);

Description

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

----------------------------------
__________________
💻Know Our New Blog👄
🔗tube2downs.blogspot.com
WATCH_D0GS UNITED is offline
tedaimlocks
Member
Join Date: Jan 2024
Old 04-16-2024 , 02:34   Re: Choosing a random player
Reply With Quote #7

Quote:
Originally Posted by mlibre View Post
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

Last edited by tedaimlocks; 04-16-2024 at 03:42.
tedaimlocks is offline
mlibre
Veteran Member
Join Date: Nov 2015
Location: return PLUGIN_CONTINUE
Old 04-16-2024 , 08:17   Re: Choosing a random player
Reply With Quote #8

Quote:
Originally Posted by WATCH_D0GS UNITED View Post
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 View Post
...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
    }
        //... }
__________________

Last edited by mlibre; 04-16-2024 at 08:24.
mlibre is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-16-2024 , 10:24   Re: Choosing a random player
Reply With Quote #9

PHP Code:
new iPlayers32 ] , iNum iRandom;
get_playersiPlayers iNum );

if ( 
iNum )
{
      
iRandom iPlayers randomiNum ) ];

__________________

Last edited by Bugsy; 04-16-2024 at 10:25.
Bugsy is offline
WATCH_D0GS UNITED
Senior Member
Join Date: Jan 2023
Old 04-16-2024 , 18:48   Re: Choosing a random player
Reply With Quote #10

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.
__________________
💻Know Our New Blog👄
🔗tube2downs.blogspot.com
WATCH_D0GS UNITED is offline
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 13:06.


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