AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   [SOLVED]Pick random player of server (https://forums.alliedmods.net/showthread.php?t=182955)

rodrigo286 04-15-2012 15:00

[SOLVED]Pick random player of server
 
I need to get all round a random player, I tried a few ways posted, but none works.

Thanks.

TheAvengers2 04-15-2012 15:56

Re: Pick random player of server
 
You can store the potential clients in an array, and then use GetURandomInt and modulus to select a random one.

Spoiler

thetwistedpanda 04-15-2012 16:23

Re: Pick random player of server
 
Same concept, but you can also use new client = numClients ? iClients[GetRandomInt(0, (numClients - 1))] : -1 as opposed to GetURandomInt.

rodrigo286 04-15-2012 16:24

Re: Pick random player of server
 
I tried but did not work, one player is randomly chosen and win the HE.

PHP Code:

#include <sourcemod>
#include <cstrike>

public OnPluginStart()
{
    
HookEvent("round_start"round_start);
}

public 
Action:round_start(Handle:event, const String:name[], bool:dontBroadcast)
{
    
GetRndClient();
}

GetRndClient()
{
    
decl iClients[MaxClients];
    new 
numClients;
    
    for (new 
1<= MaxClientsi++)
    {
        if (
IsClientInGame(i) && !IsClientSourceTV(i))
        {
            
iClients[numClients++] = i;
        }
    }
    
    if (
numClients)
    {
        return 
iClients[GetURandomInt() % numClients];
    }
    else
    {
        return -
1// no clients? let's not divide by zero. :)
    
}

    
GivePlayerItem(numClients"weapon_hegrenade");


Thanks.

TheAvengers2 04-15-2012 16:30

Re: Pick random player of server
 
Quote:

Originally Posted by rodrigo286 (Post 1689879)
I tried but did not work, one player is randomly chosen and win the HE.

PHP Code:

#include <sourcemod>
#include <cstrike>

public OnPluginStart()
{
    
HookEvent("round_start"round_start);
}

public 
Action:round_start(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
rndClient GetRndClient();
    if (
rndClient > -1)
    {
        
GivePlayerItem(rndClient"weapon_hegrenade");
    }
}

GetRndClient()
{
    
decl iClients[MaxClients];
    new 
numClients;
    
    for (new 
1<= MaxClientsi++)
    {
        if (
IsClientInGame(i) && !IsClientSourceTV(i))
        {
            
iClients[numClients++] = i;
        }
    }
    
    if (
numClients)
    {
        return 
iClients[GetURandomInt() % numClients];
    }
    else
    {
        return -
1// no clients? let's not divide by zero. :)
    
}


Thanks.

Quote:

Originally Posted by thetwistedpanda (Post 1689877)
Same concept, but you can also use new client = numClients ? iClients[GetRandomInt(0, (numClients - 1))] : -1 as opposed to GetURandomInt.

That'll work too. :P

Could also do:
Code:

return (numClients) ? iClients[GetURandomInt() % numClients] : -1;
But, I like my comment. :cry:

rodrigo286 04-15-2012 16:41

Re: Pick random player of server
 
Thanks works perfectly.


All times are GMT -4. The time now is 18:03.

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