If you wanted to make a function that just picks a random player then you could do
local module = {}
function module.ChoosePercentage()
local playersTable = game.Players:GetPlayers()
local percentage = math.random(1, #playersTable)
local randomPlayer = playersTable[percentage]
return randomPlayer
end
return module
But instead, if you wanted to do it in your way, you could think like this.
You want to separate 100 into different random parts, so you could think like this
You have a stick of length 100m
Now, for example, you have 5 friends. And you need to separate the stick into random 5 parts so each friend gets a random length of the stick.
So, you could cut up the stick 4 times in random positions to get 5 random pieces.
In the above example, the stick is cut up into random areas such as 19, 50, 58, 86.
Now that you know where you have cut up the stick you could easily find the lengths of the stick as well like this.
The length of the first stick is just the higher value(19) - lower value(0) = 19m
second stick = higher value(50) - lower value(19) = 31m
third stick = higher val(58) - lower val(50) = 8m
fourth stick = higher val(86) - lower val(58) = 28m
fifth stick = higher val(100) - lower val(86) = 15m
Now if you add up all these values i.e. 19 + 31 + 8 + 28 + 14, you get 100
So now we have separated 100 into random parts of 19m, 31m, 8m, 28m and 14m.
Now you can apply the same in scripting like this -
local module = {}
function module.ChoosePercentages()
local randomNumbers = {}
local percentages = {}
local playersTable = game.Players:GetPlayers()
local noOfPlayers = #playersTable
for i = 1, noOfPlayers - 1 do
local randomNumber = math.random(1, 100)
table.insert(randomNumbers, randomNumber)
end
table.insert(randomNumbers, 0)
table.insert(randomNumbers, 100)
table.sort(randomNumbers)
for i, player in pairs(playersTable) do
local percentage = randomNumbers[i+1] - randomNumbers[i]
percentages[player] = percentage
end
local luckyNumber = math.random(1, 100)
local xNumber = 0
for player, percentage in pairs(percentages) do
xNumber = xNumber + percentage
if luckyNumber <= xNumber then
return player
end
end
end
return module
The above is the script that you will need.
Now I will go through each line so you can understand what is happening.
local randomNumbers = {}
local percentages = {}
local playersTable = game.Players:GetPlayers()
local noOfPlayers = #playersTable
The above are just some variables you will need.
for i = 1, noOfPlayers - 1 do
local randomNumber = math.random(1, 100)
table.insert(randomNumbers, randomNumber)
end
The above lines are just like this. For example, you have 10 friends, so you need to cut the stick 9 times(10 - 1)
The randomNumber is just the area to cut the stick and we can store these numbers in a table.
table.insert(randomNumbers, 0)
table.insert(randomNumbers, 100)
table.sort(randomNumbers)
Now in the above, we just need to add 0 and 100 to the table for calculation purpose.
And then we need to sort the table from 0 to 100 in ascending order.
The reason to sort them in ascending order is for calculation (remember? (higher value - lower value))
And now we have the areas we cut the stick, we need to calculate the length of broken sticks.
for i, player in pairs(playersTable) do
local percentage = randomNumbers[i+1] - randomNumbers[i]
percentages[player] = percentage
end
So, for example, the first cut we made is at 8m and the second cut is at 22m
So then the length of the first stick is just 8 - 0 (2nd val - 1st val)
And the length of the second stick is 22 - 8 (3rd val - 2nd val)
and so on.
So that’s how we calculated the percentages and we can store them in the percentages table.
local luckyNumber = math.random(1, 100)
local xNumber = 0
for player, percentage in pairs(percentages) do
xNumber = xNumber + percentage
if luckyNumber <= xNumber then
return player
end
end
Then the above rest is just your method of selecting the player i.e. making a luckyNumber and a xNumber and adding percentages to xNumber until it is smaller than or equal to the luckyNumber.
Sorry for making this post so extremely long and in case you didn’t understand anything, just copy the long script.