So i am working on a game kinda like murder mystery and i want to add role chances into the game so you have a murderer chance and a sheriff chance, how would i go about doing this?
Please search before posting!
You can go about this in two ways: crudely or elegantly.
When you’re working with chances in games, whether it’s to select roles like a Murder Mystery type game or for loot dropping in RPGs, you’ll typically be faced with tables and randomisation, so this is where you’ll want to begin your searching first.
If you’re looking for a super crude way of doing this, then you can just make a giant table where the player’s name is repeated n amount of times where n represents the algorithm you’re using (can be as simple as multiplying by 100) and a selection is made from that table for roles. This is either incremented or reset depending on if the player is selected for a role or not. Example:
local myKillerChance = 50
local killerPick = {}
for i = 1, myKillerChance do
table.insert(killerPick, "Player1")
end
If you’re looking for the elegant way, then I would recommend that you look into weighted tables. They don’t deal in terms of percentage necessarily but you can get a percentage from it. You can think of it like a loot table where there’s three loots: murderer, sheriff and nothing (innocent). At least one player should get murderer and sheriff (so reroll if you need to) and then the rest pick up innocent.
There are probably a few resources around that you can look at to help you construct this. For example, there is a tutorial series from Roblox University back when Murder Mystery games were a popular trend on Roblox. It goes through the steps of understanding and creating a Murder Mystery game and it also includes a complete copy that you can look through.
The template is available here if you understand code and want to skip straight to getting your hands dirty (try searching for the chance code to get an understanding of how they do it):
If you’d like to watch the whole tutorial series to get a grasp on creating this game and learn a bit of how they get things done along the way, I encourage you to check out the playlist. It is in fact called How to Make a “Murder Mystery” Game:
And as others pointed out above, there are existing threads on this topic that you can get some information from as well.