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.
1 | players = { } |
2 |
3 | function start() |
4 | wait( 1 ) |
5 | players = game.Players:GetPlayers() |
6 | local selected = players [ math.random( 1 , #players) ] .Name |
7 | print (selected) |
8 | end |
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:
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:
01 | local weights = { } ; |
02 | local totalWeight = 0 ; |
03 | -- 1) Count number of raffle tickets |
04 | -- (can actually be non-integer, too) |
05 | for 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 ] ; |
14 | end |
15 |
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.
1 | for 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 |
6 | end |
To add on to what red said, you could also create a table and if the player has the pass, insert their name twice.
1 | local players = { } |
2 |
3 | for 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) |
8 | end |