Raised This Month: $32 Target: $400
 8% 

Get_players stock


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Crackhead69
Member
Join Date: Feb 2021
Old 04-24-2021 , 12:07   Get_players stock
Reply With Quote #1

Hello!
I am trying to figure out how to make a get_players stock which can be used like so
PHP Code:
new players

public plugin_init() {
    
register_clcmd("say /test","TestStock")
}

public 
TestStock(id){
    if(!
is_user_alive(id) || !is_user_connected(id)) return
    
    
get_them_players("ae","TERRORIST")
    
    
set_user_health(players20)
}

stock get_them_players(szFlags[] = ""szTeam[] = ""){
    
players = -1
    
    
new iPlayers[32], iPnum
    get_players
(iPlayersiPnumszFlagsszTeam)
    
    for(new 
i;iiPnum;i++)
        
players iPlayers[i]

From the above code the goal is to set the HP of all the T players using the stock to 20 by calling the stock,
tho as i test it, it sets the hp to 20 on random T players, not on all of them.

I would appreciate if somebody could lay some wisdom.
Crackhead69 is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 04-24-2021 , 12:25   Re: Get_players stock
Reply With Quote #2

You will have to give health to each player in the loop, they way you are trying is not possible.
__________________
Retired.
Xalus is offline
Crackhead69
Member
Join Date: Feb 2021
Old 04-24-2021 , 12:44   Re: Get_players stock
Reply With Quote #3

Oh really? Didn't know that
So in general it is not possible to store the iPlayers[33] from get_players into a global variable?
Crackhead69 is offline
LondoN
Senior Member
Join Date: Dec 2015
Location: Roman, Romania.
Old 04-24-2021 , 12:52   Re: Get_players stock
Reply With Quote #4

Code:
stock get_and_set(szFlags[] = "", szTeam[] = "", iHealth)
{
	new Players[32], iNum;
	get_players(Players, iNum, szFlags, szTeam);

	for(new i; i < iNum; i++)
	{
		if(!iHealth)
			break;

		set_user_health(Players[i], iHealth);
	}
}
Usage:

Code:
get_and_set("ae", "TERRORIST", 250);
__________________
LondoN is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-24-2021 , 14:48   Re: Get_players stock
Reply With Quote #5

Quote:
Originally Posted by Crackhead69 View Post
Oh really? Didn't know that
So in general it is not possible to store the iPlayers[33] from get_players into a global variable?
Well, technically you can store anything in a global variable but that's not actually relevant here. You also don't want to do this because the result of get_players() will change depending on when people join and leave so you should always use get_players() when you need it so that you don't try to run functions on non-existent players (or players that no longer match the provided flags.

Also, your "players" global variable is a scalar so it will only contain a single value ever. In the for loop, you're simply overwriting that variable many times without having done anything with it and will have only the last player index in it when get_them_players() finishes executing. So, when you eventually call set_user_health() you are doing so on only that last player.

So here, based on just your code alone, the stock is unnecessary, simply put the get_players() and the loop in your main function and set the user's health for each player in that loop.

Quote:
Originally Posted by LondoN View Post
Code:
stock get_and_set(szFlags[] = "", szTeam[] = "", iHealth) {     new Players[32], iNum;     get_players(Players, iNum, szFlags, szTeam);     for(new i; i < iNum; i++)     {
        if(!iHealth)
            break;
        set_user_health(Players[i], iHealth);     } }
  1. iHealth will always be the same value in this function so it's pointless to even do any looping or even get_players() if it's just going to break out of the loop immediately.
  2. Also, a check for iHealth being zero doesn't make sense, the person calling the function should just not call the function if they don't want it to do it's function.
  3. You aren't really helping if you don't explain anything about what the OP is actually having issues with.
__________________
fysiks is offline
LondoN
Senior Member
Join Date: Dec 2015
Location: Roman, Romania.
Old 04-24-2021 , 14:54   Re: Get_players stock
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
Well, technically you can store anything in a global variable but that's not actually relevant here. You also don't want to do this because the result of get_players() will change depending on when people join and leave so you should always use get_players() when you need it so that you don't try to run functions on non-existent players (or players that no longer match the provided flags.

Also, your "players" global variable is a scalar so it will only contain a single value ever. In the for loop, you're simply overwriting that variable many times without having done anything with it and will have only the last player index in it when get_them_players() finishes executing. So, when you eventually call set_user_health() you are doing so on only that last player.

So here, based on just your code alone, the stock is unnecessary, simply put the get_players() and the loop in your main function and set the user's health for each player in that loop.


  1. iHealth will always be the same value in this function so it's pointless to even do any looping or even get_players() if it's just going to break out of the loop immediately.
  2. Also, a check for iHealth being zero doesn't make sense, the person calling the function should just not call the function if they don't want it to do it's function.
  3. You aren't really helping if you don't explain anything about what the OP is actually having issues with.
it's depend on how you use the stock.

Let's take an example:

instead of putting 250 as health (stock parameter) we also can use get_pcvar_num to take the value from a cvar.
if the cvar is zero just break the loop, this is what i wanted when i checked for !iHealth
__________________
LondoN is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 04-24-2021 , 16:01   Re: Get_players stock
Reply With Quote #7

Nonsense.

There is no need for a get_players stock thats nonsense use get_players as it supposed to be used.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 04-24-2021 at 16:02.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-25-2021 , 01:21   Re: Get_players stock
Reply With Quote #8

Quote:
Originally Posted by LondoN View Post
it's depend on how you use the stock.

Let's take an example:

instead of putting 250 as health (stock parameter) we also can use get_pcvar_num to take the value from a cvar.
if the cvar is zero just break the loop, this is what i wanted when i checked for !iHealth
You didn't put a cvar and even if you had a cvar, the cvar wont' change in the middle of the loop.
__________________
fysiks 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 04:59.


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