How to make a role amount limiter (Rarity system) in a ModuleScript?

Hey guys! I hope you all do very very well! Being direct, I’m trying to Randomize Roles based on Chance, but avoid the possibility of giving out more than X of the said role. Yet I don’t know how to set limits for roles.

I know there’s a lot and a lot of good people here, and I will appreciate your help a lot if you can give me a hand with this, even if it’s the smallest one. :wink:

ModuleScript (Rarity Giver):

CardModule.ChooseRandomCard = function()
	local randomNumber = math.random(1,100)
	local counter = 0
	for rarity, weight in pairs(CardModule.Rarities) do
		counter = counter + weight
		if randomNumber <= counter then
			
			local rarityTable = CardModule.Cards[rarity]
			local chosenCard = rarityTable[math.random(1,#rarityTable)]
			
			return chosenCard
		end
	end
end

Rarity Limit:

CardModule.MaxLimitOfRoles = {
	
	["Vampire"] = {
		MaxQuantity = 3;
	};
	
	["Villager"] = {
		MaxQuantity = 2;
	};
	
	["Troll"] = {
		MaxQuantity = 1;
	};
	
}

So what I’m trying to script is an amount limiter of each role, but I don’t find any way to do it so, I have tried many times different type of script, but they didn’t work, so If you can give me a hand with this, I’ll appreciate very very much your help. Have a nice day! bye! :wave: :grinning_face_with_smiling_eyes: :wink:

1 Like

Without knowing how the variables are named/how the system works/the names of cards this is what I came up with, essentially you need 3 variables which track the count of each card, if a random card is selected which has already been selected a maximum amount of times the function calls itself and repeats the process until a card is selected which hasn’t already reached its maximum distribution allowance at which point the card is returned by the function.

You will likely need to go through the code as it’s just an example concept.

CardModule.MaxLimitOfRoles = {

	["Vampire"] = {
		["MaxQuantity"] = 3;
	};

	["Villager"] = {
		["MaxQuantity"] = 2;
	};

	["Troll"] = {
		["MaxQuantity"] = 1;
	};

}

local vampireCards = 0
local villagerCards = 0
local trollCards = 0

CardModule.ChooseRandomCard = function()
	local randomNumber = math.random(1,100)
	local counter = 0
	for rarity, weight in pairs(CardModule.Rarities) do
		counter += weight
		if randomNumber <= counter then

			local rarityTable = CardModule.Cards[rarity]
			local chosenCard = rarityTable[math.random(1,#rarityTable)]
			if chosenCard == "Vampire" then
				vampireCards += 1
			elseif chosenCard == "Villager" then
				villagerCards += 1
			elseif chosenCard == "Troll" then
				trollCards += 1
			end
			
			if vampireCards > CardModule.MaxLimitOfRoles["Vampire"]["MaxQuantity"] or villagerCards > CardModule.MaxLimitOfRoles["Villager"]["MaxQuantity"] or trollCards > CardModule.MaxLimitOfRoles["Troll"]["MaxQuantity"] then
				CardModule.ChooseRandomCard() --recursive (calls function again)
			else
				return chosenCard
			end
		end
	end
end
1 Like

Thank you so much for your help! and sorry for didn’t answering to you in this long time that has passed, I thought nobody would help me, but it’s totally not the case! thank you so much! There’s still another error that says that it can’t Index nil with “MaxQuantity” but I didn’t managed to fix it, if you can and still want to help me (even when I was a little piece of trash for making you wait this long time) go to my new post:

Special thanks!

:wave: :smiley: :wink: