Raised This Month: $ Target: $400
 0% 

Getting alive players count not working?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
flamingkirby
Junior Member
Join Date: Nov 2013
Old 11-25-2013 , 21:47   Getting alive players count not working?
Reply With Quote #1

Hello everyone. I am puzzled as to why this is not working. I saw other people use this exact same code but when i die, it says i am still alive.

Code:
public OnPluginStart()
{
	HookEvent("player_death", EVENT_playerDeath);
}

public EVENT_playerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if(getAliveClients() == 0) PrintToChatAll("All clients are dead!");
}

public getAliveClients()
{
	new count = 0;
	
	for(new i = 1; i <= MaxClients; i++)
	{
		if(IsFakeClient(i)) continue; //Ignores bots
		new name[32];
		GetClientName(i, name, 32);
		if(IsPlayerAlive(i) && IsPlayerInGame(i)) count++; PrintToChatAll("%s is alive", name);
	}
	
	return count;
}
The only thing that i may see wrong with this is the event, it is set at default, post which should happen after the event is fired so i should be declared dead in game but for some weird reason it says i am still alive. Can someone please see what is wrong with this code. Thanks.
__________________
flamingkirby is offline
Sillium
AlliedModders Donor
Join Date: Sep 2008
Location: Germany
Old 11-26-2013 , 01:15   Re: Getting alive players count not working?
Reply With Quote #2

The PrintToChatAll is always executed because it is not part of the if clause:
Code:
if(IsPlayerAlive(i) && IsPlayerInGame(i)) count++; PrintToChatAll("%s is alive", name);
should probably be:
Code:
if(IsPlayerAlive(i) && IsPlayerInGame(i))
{
   count++;
   PrintToChatAll("%s is alive", name);
}
__________________
brb, dishes have developed their own language and are talking to the garbage about overthrowing me... i must correct this

www.unterwasserpyromanen.de
Sillium is offline
flamingkirby
Junior Member
Join Date: Nov 2013
Old 11-26-2013 , 01:26   Re: Getting alive players count not working?
Reply With Quote #3

Ye, just for debugging to see what it is printing. I think the problem was that i did not have the hook event in pre as when i changed it to pre, it seemed to work but just need a bigger crowd to test it fully.
__________________
flamingkirby is offline
GsiX
gee, six eggs
Join Date: Aug 2012
Location: Land Below The Wind
Old 11-26-2013 , 02:06   Re: Getting alive players count not working?
Reply With Quote #4

Do as Silium suggest.. otherwise you going no where..
Its so obvious you dead or alive, you will be declared alive.
__________________
If i happen to insulted you unintentionally,
it was me and Google Translate who did it.
GsiX is offline
Versace14
New Member
Join Date: Nov 2013
Old 11-26-2013 , 02:12   Re: Getting alive players count not working?
Reply With Quote #5

I am extremely happy to read about moltivite.. I need to read about this completely..
I'll share this info to my friends too..
__________________

Last edited by Versace14; 01-12-2016 at 00:14.
Versace14 is offline
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 11-26-2013 , 13:10   Re: Getting alive players count not working?
Reply With Quote #6

If you're going to be doing something with the alive clients, you could do this:
PHP Code:
public getAliveClients(String:&targets[])
{
    
decl String:target_name[MAX_TARGET_LENGTH];
    
decl target_count;
    
decl bool:tn_is_ml;
    if ((
target_count ProcessTargetString(
            
"@all",
            
0,
            
targets,
            
MAXPLAYERS,
            
COMMAND_FILTER_ALIVE|COMMAND_FILTER_NO_BOTS,
            
target_name,
            
sizeof(target_name),
            
tn_is_ml)) <= 0)
                return 
0;
    return 
target_count;

Haven't tested, and also have never used a reference variable in PAWN, so not sure if the format is correct.. But you give it a string array with size of MAXPLAYERS and it will be filled with the alive non-bot players, and the return value will be the number of matching clients.

Last edited by NIGathan; 11-26-2013 at 13:15.
NIGathan is offline
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 11-27-2013 , 10:23   Re: Getting alive players count not working?
Reply With Quote #7

No need to hook the event in pre... the action of player death has already occurred, but the event is just the notification.

PHP Code:
public OnPluginStart()
{
    
HookEvent("player_death"EVENT_playerDeathEventHookMode_PostNoCopy);
}

public 
EVENT_playerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
    if (
getAliveClients() == 0)
    {
        
PrintToChatAll("All clients are dead!");
    }
}

getAliveClients()
{
    new 
count 0;
    
    for (new 
1<= MaxClientsi++)
    {
        if (!
IsClientInGame(i) || IsFakeClient(i) || !IsPlayerAlive(i))
        {
            continue; 
//Check if client (i) is in game and Ignores bots and dead players
        
}
        
        
count++; 
        
PrintToChatAll("%N is alive"i);
    }
    
    return 
count;

And you don't need to declare a string and use get the player's name, just use the format %N with the client ID and it will give you the player's name...

Also, you need to always check if IsClientInGame whenever you're looping through all client IDs if you're going to work with client IDs otherwise, some of the calls/checks you make will error out on invalid clients.
__________________
View my Plugins | Donate

Last edited by TnTSCS; 11-27-2013 at 15:08.
TnTSCS is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 11-27-2013 , 14:29   Re: Getting alive players count not working?
Reply With Quote #8

Not sure why new client = ... is even needed, unless you left some code out. If you haven't then, as TnTSCS suggested, you can use EventHookMode_PostNoCopy.
__________________
11530 is offline
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 11-27-2013 , 15:08   Re: Getting alive players count not working?
Reply With Quote #9

True - sample code updated
__________________
View my Plugins | Donate
TnTSCS is offline
GsiX
gee, six eggs
Join Date: Aug 2012
Location: Land Below The Wind
Old 11-28-2013 , 06:47   Re: Getting alive players count not working?
Reply With Quote #10

Otherwise, nearest local hospital provide "death declaration certificate".
GsiX 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 05:54.


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