I am trying to work out how to do this. How would i figure out which person has the higher percentage and what if the percentage are the same.
Make a table, and then, and then check each player percentage, for each 1% they have, add him once to the table, for example a person which has 20%, gets added 20 times into the table, and a person which has 5% gets added 5 times and like this, then use this function to grab a random value from a table
tablename[math.random(1,#tablename)]s
So, each player should have a percentage. How you do this is up to you, but you must have them add up to 100. Here’s a example:
--This data should be generated by one of your scripts
local pickChances = {
{
player = game.Players.Bob,
chance = 50,
},
{
player = game.Players.Bill,
chance = 15,
},
{
player = game.Players.Jack,
chance = 35,
},
}
local endResult = {}
for _, data in pairs(pickChances) do
for c = 1, data.chance, 1 do
table.insert(endResult,(data.player))
end
end
local chosenIndex= math.random(1, #endResult)
local chosen = endResult[chosenIndex]
And there you have it. All you need to do is feed this percentages, and equal choices will be made. While it should add up to 100%, this method will work with any number (25 in a total of 125 would be equal to 20%)
Store a table containing all players (ServerScriptService preferably), after every round, every player will gain 1 “probability point” (besides “it” player, which will be reset to 1 “probability point”)
(Player1’s “probability points”) divided by (summation of ALL “probability points”) * 100 = the percent chance that player has of being picked.
now, we must pick a number from 0 to (summation of ALL “probability points”) and iterate through the player list, adding up the given players “probability points” starting from 0,until the randomly generated number ends up being less than the progressive summation of the player’s “probability points”.
Here’s how the script may look when done using this method (Does not account for players chosen to be “it” or any monetization methods):
local Players = game:GetService("Players")
local P_Summation = 0
local Probabilities = {}
Players.PlayerAdded:Connect(function(plr) --When player joins
table.insert(Probabilities, {plr.Name, 1}) -- player starts with 1 "probability pt"
P_Summation = P_Summation + 1 -- let's just make it easier to find the summation
end)
Players.PlayerRemoving:Connect(function(plr)
for i, v in pairs(Probabilities) do
if plr.Name == v[1] then -- iterate through entire table to find the player
P_Summation = P_Summation - v[2] -- subtract the leaving plrs "prob pts"
table.remove(Probabilities,i) -- finally, delete their place in the table
end
end
end)
function P_Random(RNG) --RNG is the number input to look for
local ToSummation = 0 -- will start 0, counts up to the total "prob pts"
for i, v in pairs(Probabilities) do --iterate through all plrs
ToSummation = ToSummation + v[2] -- v[2] = the given plr's "prob pts"
if RNG<ToSummation then
return v -- returns an ARRAY from the Probabilities array
end
end
end
-- BindableEvent signalling round end
game.ServerStorage.BindablesFolder.RoundEnd.Event:Connect(function()
for i,v in pairs(Probabilities) do
v[2] = v[2] + 1
P_Summation = P_Summation + 1
end
end)
print(P_Random(math.random()*P_Summation)[1]) -- prints name of player given float 0 - summation
-- use the function to aid in picking a player, where needed
i keep getting player is not a valid member of Player
Since that was a example, no one is actually called Bob in the game.
As a result, it is not a valid member as it doesn’t exist.
Yea i have created my own table, but when i use endResult.push(data.player) either no player is valid or the push is an error.
Something must be wrong on your end, as data is a table not a Player. Is it throwing on a line that isn’t part of that code? Did you modify the code?
Please show that relevant code or we can not help you. My code works perfectly fine in studio and game. You are doing something differently or wrong.
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local chance = Instance.new("IntValue")
chance.Name = "Chance"
chance.Value = 1
chance.Parent = folder
end
local runners = {}
for PlayerName, Data in next, RoundPlayers do
if RoundPlayers[PlayerName].Alive and not RoundPlayers[PlayerName].Tagger then
runners[#runners+1] = game.Players[PlayerName]
end
end
local endResult = {}
for _, data in pairs (runners) do
for c = 1, data.leaderstats.Chance.Value,1 do
endResult.push(data.player)
end
end
local chosenIndex= math.random(1, #endResult)
local chosen = endResult[chosenIndex]
Infect(chosen)
You’re trying to call endResult.push
, which isn’t a thing in Lua unless you put a function called push
inside of endResult
that does what table.insert
would do.
This is also not an efficient way to implement this. Here is another thread where this problem was solved Using percentages with math.random() - #4 by Rezault
Sorry, I’ve been doing a lot of javascript lately; My edited code should work now.