Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

Randomly Select Player?

Asked by 10 years ago

I have been trying to think of a way to randomly select a player, which I can do, but I can't figure out a way to give people a better chance of being picked if they have a specific gamepass. I'm not quite sure how I would be able to do this, does anybody know a way how to do this? Any help would be appreciated. I have provided the code I use to randomly select one player, all I need to do is find a way to increase the odds for somebody that has the gamepass without making them selected too often or not as often.

1players = {}
2 
3function start()
4    wait(1)
5    players = game.Players:GetPlayers()
6    local selected = players[math.random(1, #players)].Name
7    print(selected)
8end

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

This problem is to find a random choice, given different weights to each choice.

Think of the weight as the number of raffle tickets each player has. Let's say normal users have 1, but players with a game pass have 10 (though this is easy to configure).

This model works like this:

  • Only one player can win
  • Everyone, even with poor odds, has a small chance to win
  • If many people have low odds, together they can have higher odds than someone else (e.g., doesn't guarantee something like 50% of raffles go to owners of the gamepass if the non-owners significantly outnumber)

To figure out who wins, we pick a random number from the total number of raffle tickets, rather than the total number of players.

To figure out which player has that ticket, we just assign the tickets sequentially to each player. That is, the first one (or ten) is the first player, the next one (or ten) is the next player's, and so on.


An implementation looks like this:

01local weights = {};
02local totalWeight = 0;
03-- 1) Count number of raffle tickets
04-- (can actually be non-integer, too)
05for i,v in pairs(players) do
06    weights[i] = 1;
07    if if game:GetService("GamePassService"):PlayerHasPass(v,ID) then
08        weights[i] = 10;
09        -- 10 times more likely than non-owners to win
10    end
11    -- Weight can include anything else that would influence
12    -- odds, including other upgrades.
13    totalWeight = totalWeight + weights[i];
14end
15 
View all 31 lines...
Ad
Log in to vote
0
Answered by
RedCombee 585 Moderation Voter
10 years ago

I would suggest using a for loop, checking through all the players for a gamepass. If they have it, they can have a (Percent) greater chance to be selected.

1for i,v in pairs(game.Players:GetPlayers()) do
2    if game:GetService("GamePassService"):PlayerHasPass(v,ID) then
3        percentage = Instance.new("NumberValue",v)
4        percentage.Value = #game.Players:GetPlayers() / 100 + Percent -- Based on how many players are in-game and how much you want the percent to increase, you can pull off a fraction of other players' perentages in upcoming code to make it equal to 100%.
5    end
6end
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

To add on to what red said, you could also create a table and if the player has the pass, insert their name twice.

1local players = {}
2 
3for i,v in pairs(game.Players:GetPlayers()) do
4    if game:GetService("GamePassService"):PlayerHasPass(v,ID) then
5        players:insert(v.Name)
6    end
7    players:insert(v.Name)
8end

Answer this question