Random choose
#1

Hello.How can I choose a random player using "foreach" and give him 5000$,for example?
I found some threads where ****** replyed,but I didn't understand perfectly.
Thank you!
Reply
#2

well here you go

pawn Код:
stock SelectRandomPlayer()
{
     new random = Random(MAX_PLAYERS);
     if(IsPlayerConnected(random))
     {
           return random;
     }
     else
     {
           SelectRandomPlayer();
     }
}
Now you can use it for example like this:

pawn Код:
new randomplayer = SelectRandomPlayer();
GivePlayerMoney(randomplayer, 5000);



+rep if that helps
Reply
#3

you can use this as a cmd...
Код:
new onlinePlayers ,winner;

for(new i = 0; i < MAX_PLAYERS ; i++)
{
    if(IsPlayerConnected(i) ) onlinePlayers++;
}

winner = random(onlinePlayers);

GivePlayerMoney(winner ,5000);
SendClinetMessage(winner ,COLOR_GREEN ,"Congrats ,you won 5000$ on a random draw!");
Reply
#4

You can choose a random Iter from foreach, using:

pawn Код:
Iter_Random(Player);
will return a random id of the list of connected players.
Reply
#5

Quote:
Originally Posted by DeathTone
Посмотреть сообщение
You can choose a random Iter from foreach, using:

pawn Код:
Iter_Random(Player);
will return a random id of the list of connected players.
so ...I have this command:randomgivemoney

PHP код:
#include <a_samp>
#include <ZCMD>
#include <foreach>
CMD:randomgivemoney(playerid,params[])
{
    
Iter_Random(Player);
    
GivePlayerMoney(Player,5000);
    return 
1;

It's this right?
EDIT:The other 2 guys, I said clearly: I am using Foreach.Simple loops is looping through offline players.
Reply
#6

Quote:
Originally Posted by SumX
Посмотреть сообщение
so ...I have this command:randomgivemoney

PHP код:
#include <a_samp>
#include <ZCMD>
#include <foreach>
CMD:randomgivemoney
{
    
Iter_Random(Player);
    
GivePlayerMoney(Player,5000);
    return 
1;

It's this right?
Actually, this is correct:
PHP код:
CMD:randomgivemoney
{
    new 
randomplayer;
    
randomplayer Iter_Random(Player);
    
GivePlayerMoney(randomplayer,5000);
    return 
1;

Reply
#7

Quote:
Originally Posted by DeathTone
Посмотреть сообщение
Actually, this is correct:
PHP код:
CMD:randomgivemoney(playerid,params[])
{
    new 
randomplayer;
    
randomplayer Iter_Random(Player);
    
GivePlayerMoney(randomplayer,5000);
    return 
1;

Thank you very much!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)