Rarity Module Help

So I have this Rarity Module, I want to multiply all the rarities by a attribute the player has called Luck Multiplier (for anyone curious the multiplier is 1.1) but I’m getting this error
image

The error is on line 39 where its multiplying

local ServerStorage = game:GetService("ServerStorage")

local Drops = ServerStorage:WaitForChild("Drops")

local Rarity = {}

Rarity.Parts = {
	
	["Ultra Mythic"] = Drops.UltraMythicPart;
	["Mythic"] = Drops.MythicPart;
	["Legendary"] = Drops.LegendaryPart;
	["Epic"] = Drops.EpicPart;
	["Super Rare"] = Drops.SuperRarePart;
	["Rare"] = Drops.RarePart;
	["Common"] = Drops.CommonPart;
}

Rarity.Rarities = {
	
	["Ultra Mythic"] = .05;
	["Mythic"] = .5;
	["Legendary"] = 1.4;
	["Epic"] = 8;
	["Super Rare"] = 14;
	["Rare"] = 20.05;
	["Common"] = 58;
	
}

function Rarity.GetPart(player)
	
	local randomNum = math.random(1,100)
	local counter = 0
	
	for _, rare in pairs(Rarity.Rarities) do
		
		if Rarity.Rarities[rare] ~= "Common" then
			
			Rarity.Rarities[rare] *= player:GetAttribute("LuckMultiplier")
			
		end
		
	end
	
	for rarity, weight in Rarity.Rarities do
		
		counter += weight
		
		if randomNum <= counter then
			
			return Rarity.Parts[rarity]
			
		end
		
	end
	
end

return Rarity

First of all, you are comparing the value here not the index. if Rarity.Rarities[rare] ~= "Common" then. Also, try printing player:GetAttribute("LuckMultiplier") as it seems like it is nil.

How would I get the value?

charrrrrr

I printed the attribute and its not nil

You need to get the index. The index in your case is _. Rename it to something else so it is clearer.

What was printed? I need to see the result.

the number 1 was printed

charrrrrrrrrrThis text will be blurred

In the for loop you did _, rare. The _ represents the index and the ‘rare’ represents the value, so Rarity.Rarities[rare] does not exist because you are searching the table for a number value. Instead, change the _, rare to Rareness, Percentage, with the Rareness value representing the name (common, uncommon, etc) and the percentage representing, well, the percentage.

now its giving me this

new script

local ServerStorage = game:GetService("ServerStorage")

local Drops = ServerStorage:WaitForChild("Drops")

local Rarity = {}

Rarity.Parts = {
	
	["Ultra Mythic"] = Drops.UltraMythicPart;
	["Mythic"] = Drops.MythicPart;
	["Legendary"] = Drops.LegendaryPart;
	["Epic"] = Drops.EpicPart;
	["Super Rare"] = Drops.SuperRarePart;
	["Rare"] = Drops.RarePart;
	["Common"] = Drops.CommonPart;
}

Rarity.Rarities = {
	
	["Ultra Mythic"] = .05;
	["Mythic"] = .5;
	["Legendary"] = 1.4;
	["Epic"] = 8;
	["Super Rare"] = 14;
	["Rare"] = 20.05;
	["Common"] = 58;
	
}

function Rarity.GetPart(player)
	
	print(player:GetAttribute("LuckMultiplier"))
	
	local randomNum = math.random(1,100)
	local counter = 0
	
	for Rarity, percentage in pairs(Rarity.Rarities) do
		
		if Rarity.Rarities[Rarity].percentage ~= "Common" then
			
			Rarity.Rarities[Rarity].percentage *= player:GetAttribute("LuckMultiplier")
			
		end
		
	end
	
	for rarity, weight in Rarity.Rarities do
		
		counter += weight
		
		if randomNum <= counter then
			
			return Rarity.Parts[rarity]
			
		end
		
	end
	
end

return Rarity

Instead of that, do

Rarity ~= "Common"

And for the other one

percentage *= player:GetAttribute("LuckMultiplier")

1 Like