How do I make a chance system

This might be a lot to ask, but I am wondering how to make a chance system.
Kind of like Murder Mystery 2, but without the Sheriff Role. Just the Innocent, and Murder Roles.

The chance of the player to be the Murder gets increased after each round, and resets back to 0% after the player has been chosen as the Murder (depends on how many players are in the server)

Any help is appreciated!

2 Likes

I’d do this by giving everyone a “chance point” which increases every time they’re selected as Innocent.

for _,plr in pairs(game.Players:GetChildren()) do
local chancePoints = instance.new("IntValue")
chancePoints.Parent = plr
if gameStarts && plr ~= Murderer then
   chancePoints.Value += 1
end

Now that we know peoples chances we can make a table using these chances and adding the player as many times as they have chances.

local plrChances = {}
for i,plr in pairs(game.Players:GetChildren()) do
   plrChances.insert(plr)
end
--This table should end up looking something like:
plrChances = {
Plr1,
Plr2,
Plr2,
Plr3,
Plr1,
Plr1
}
Some people are in there more times than others. Do you see where this is heading?

Now that we have a table, we can run a randomizer that picks a random number within the table index.

local selectedPlayer = math.Random(1, #plrChances)
changePlayerToMurderer(plrChances[selectedPlayer])
--This should work because every name inserted into the table is also has a
--number that comes with it(index). So by calling calling 
--selectedPlayer(Number) inside of plrChances(our table), is essentially the
--same as saying "Pick this person inside of the table".  

--make sure to make the players chance to become murderer goes back down
plrChances[selectedPlayer].chancePoints = 1

Murderer will now be person assigned the number is landed on!
This is mainly a general idea of what should be happening, attempting to copy and paste these lines will not work. (I also probably accidentally mixed-up commands Lua and JavaScript can do.) If you’re confused on what some of the things Im using are I’ll make sure to put links to explanations so you can read up on them : )

Tables | Roblox Creator Documentation
Random | Roblox Creator Documentation
Could somebody explain for i, v in pairs - Help and Feedback / Scripting Support - DevForum | Roblox

1 Like

You might want to research weighted chances.

I made a module that does this before for an AI system and it is likely what you are looking for.

3 Likes

I had to tweak a few things, but now it works perfectly, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.