• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Random Players
#1
Hey there, I'm new to scripting, so I was wondering if anyone could help me, please.



Basically, I would need a script that would randomly select 5 players, out of all players on the server.



In the script, selected?players shouldn't repeat. I have 0 idea of how I could do it, so if anyone has any time to spare and help me out, it would be awesome.



Thank you!
  Reply
#2
new UsedID[MAX_PLAYERS];
new counter;

foreach(new i : Player)
{
if(counter >= 5) break;
new RandomID = random(i);
if(!IsPlayerConnected(i));
UsedID[RandomID] = 1;
counter;
}
<removed giant sig>
  Reply
#3
You can use y_foreach and use the Iter_Random function.

https://github.com/pawn-lang/YSI-Include...ach/api.md

search Iter_Random, it'll give you options of exclusions so you can include the already random players.
  Reply
#4
I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.



1. What if there is only 5 online. We fail the function or pick them ?

2 What if there is only 1 or 2 people online, do we pick them or fail the function ?
  Reply
#5
(2020-10-25, 04:13 PM)Expert* Wrote: I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.



1. What if there is only 5 online. We fail the function or pick them ?

2 What if there is only 1 or 2 people online, do we pick them or fail the function ?



Oh, that would be lovely if you can, since I don't understand much in coding.



Well, if possible, if there are 10 people, the function would only work then.



Also, players shouldn't repeat in that function, if that's possible as well.



And thank you, and everyone else for answering!
  Reply
#6
(2020-10-25, 05:59 PM)Pr0fit Wrote:
(2020-10-25, 04:13 PM)Expert* Wrote: I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.

1. What if there is only 5 online. We fail the function or pick them ?
2 What if there is only 1 or 2 people online, do we pick them or fail the function ?

Oh, that would be lovely if you can, since I don't understand much in coding.

Well, if possible, if there are 10 people, the function would only work then.

Also, players shouldn't repeat in that function, if that's possible as well.

And thank you, and everyone else for answering!
I'm on it, I'll edit the comment once I finish writing the function as I'm writing on my android

Code:
randomPlayerFunction()
{
    const
        MAX_RND_PLAYERS = 5,
        MIN_RND_ONLINE = 10;
        
    if (Iter_Count(Player) < MIN_RND_ONLINE) return 0;
    
    new
        tmpIDs[MAX_RND_PLAYERS] = INVALID_PLAYER_ID,
        counter;
        
    while(tmpIDs[MAX_RND_PLAYERS - 1] == INVALID_PLAYER_ID)
    {
        new
            val = Iter_Random(Player);
            
        for(new i = 0; i < MAX_RND_PLAYERS; )
        {
            if(tmpIDs[i] == val)
                break;
                
            if(i == MAX_RND_PLAYERS)
            {
                tmpIDs[counter] = val;
                counter ;
            }
        }
    }
    
    for(new id = 0; id < MAX_RND_PLAYERS; 﨧)
    {
        // code
        // usage ex
        printf("ID: %i", tmpIDs[id]);
    }
    return 1;
}

This should work
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply
#7
(2020-10-25, 07:56 PM)Pinch Wrote:
(2020-10-25, 05:59 PM)Pr0fit Wrote:
(2020-10-25, 04:13 PM)Expert* Wrote: I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.



1. What if there is only 5 online. We fail the function or pick them ?

2 What if there is only 1 or 2 people online, do we pick them or fail the function ?



Oh, that would be lovely if you can, since I don't understand much in coding.



Well, if possible, if there are 10 people, the function would only work then.



Also, players shouldn't repeat in that function, if that's possible as well.



And thank you, and everyone else for answering!

I'm on it, I'll edit the comment once I finish writing the function as I'm writing on my android



Code:
randomPlayerFunction()

{

? ? const

? ? ? ? MAX_RND_PLAYERS = 5,

? ? ? ? MIN_RND_ONLINE = 10;

? ? ? ?

? ? if (Iter_Count(Player) < MIN_RND_ONLINE) return 0;

? ?

? ? new

? ? ? ? tmpIDs[MAX_RND_PLAYERS] = INVALID_PLAYER_ID,

? ? ? ? counter;

? ? ? ?

? ? while(tmpIDs[MAX_RND_PLAYERS - 1] == INVALID_PLAYER_ID)

? ? {

? ? ? ? new

? ? ? ? ? ? val = Iter_Random(Player);

? ? ? ? ? ?

? ? ? ? for(new i = 0; i < MAX_RND_PLAYERS; )

? ? ? ? {

? ? ? ? ? ? if(tmpIDs[i] == val)

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ?

? ? ? ? ? ? if(i == MAX_RND_PLAYERS)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? tmpIDs[counter] = val;

? ? ? ? ? ? ? ? counter ;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ?

? ? for(new id = 0; id < MAX_RND_PLAYERS; 﨧)

? ? {

? ? ? ? // code

? ? ? ? // usage ex

? ? ? ? printf("ID: %i", tmpIDs[id]);

? ? }

? ? return 1;

}



This should work

I will give it a try tomorrow. Thank you so much! :)
  Reply


Forum Jump: