Have ores weighted

I was reading through this post and was struggeling to get it work really as intended. For example, if players depth is 99, then it only has 7 ores appear

GENERATING ▼ {
[1] = “Uranium Ore”,
[2] = “Opal Ore”,
[3] = “Amethyst Ore”,
[4] = “Platinum Ore”,
[5] = “Emerald Ore”,
[6] = “Diamond Ore”,
[7] = “Sapphire Ore”
} - Edit
Which means Uranium/Opal have the same odds as the other ores. These ores however should still be quite rare. And thus, players are currently able to find these ores really easily if they just get down to the 90s depth

local function GetDepthScore(ore, depth)
	if depth > ore.MaxDepth or depth < ore.MinDepth then
		return 0 -- Can't spawn
	elseif depth < ore.DepthApex then -- Below apex (higher up)
		return (depth - (ore.MinDepth - 1)) / (ore.DepthApex - (ore.MinDepth - 1)) 
	elseif depth == ore.DepthApex then -- At apex
		return 1
	elseif depth > ore.DepthApex then -- Above apex (deeper down)
		return ((ore.MaxDepth + 1) - depth) / (ore.MaxDepth + 1)
	end
end

local MineData = require(game.ReplicatedStorage.MineData)
local Depth = 99
local OreOdds = {}
for ore, oreData in pairs(MineData) do
	local DepthScore = GetDepthScore(oreData, Depth)
	if DepthScore <= 0 then continue end -- No ore at this depth

	local Percent =  DepthScore * 100

	-- Add to OreOdds
	for i = 1, Percent do
		table.insert(OreOdds, ore)
	end
end

if #OreOdds > 0 then -- Spawn an ore
	local RandomOre = OreOdds[math.random(1, #OreOdds)]
	print("GENERATING", OreOdds)
end
return {
	["Coal Ore"] = {
		DepthApex = 14,
		MinDepth = 7,
		MaxDepth = 65,
	},
	["Copper Ore"] = {
		DepthApex = 22,
		MinDepth = 10,
		MaxDepth = 70,
	},
	["Silver Ore"] = {
		DepthApex = 26,
		MinDepth = 10,
		MaxDepth = 75,
	},
	["Iron Ore"] = {
		DepthApex = 32,
		MinDepth = 10,
		MaxDepth = 80,
	},
	["Gold Ore"] = {
		DepthApex = 42,
		MinDepth = 20,
		MaxDepth = 85,
	},
	["Ruby Ore"] = {
		DepthApex = 49,
		MinDepth = 20,
		MaxDepth = 90,
	},
	["Quartz Ore"] = {
		DepthApex = 56,
		MinDepth = 30,
		MaxDepth = 95,
	},
	["Sapphire Ore"] = {
		DepthApex = 62,
		MinDepth = 40,
		MaxDepth = 100,
	},
	["Diamond Ore"] = {
		DepthApex = 69,
		MinDepth = 50,
		MaxDepth = 100,
	},
	["Emerald Ore"] = {
		DepthApex = 76,
		MinDepth = 60,
		MaxDepth = 100,
	},
	["Amethyst Ore"] = {
		DepthApex = 82,
		MinDepth = 70,
		MaxDepth = 100,
	},
	["Platinum Ore"] = {
		DepthApex = 86,
		MinDepth = 80,
		MaxDepth = 100,
	},
	["Opal Ore"] = {
		DepthApex = 93,
		MinDepth = 90,
		MaxDepth = 100,
	},
	["Uranium Ore"] = {
		DepthApex = 96,
		MinDepth = 90,
		MaxDepth = 100,
	},
}
local Ores = {
	["Uranium Ore"] = {1, 1}, --1%
	["Opal Ore"] = {2, 5}, --4%
	["Amethyst Ore"] = {6, 15}, --10%
	["Platinum Ore"] = {16, 30}, --15%
	["Emerald Ore"] = {31, 50}, --20%
	["Diamond Ore"] = {51, 70}, --20%
	["Sapphire Ore"] = {71, 100} --30%
}

local function GetRandomOreWeighted()
	local RandomNumber = math.random(1, 100)
	for OreName, OreArray in pairs(Ores) do
		if RandomNumber >= OreArray[1] and RandomNumber <= OreArray[2] then
			local Chance = (OreArray[2] - OreArray[1]) + 1
			return OreName, Chance
		end
	end
end

local OreName, Chance = GetRandomOreWeighted()
print("You found a "..OreName..", it has a rarity of "..Chance.." percent.")

Converted your first table into a weighted one, hopefully what I changed should be fairly simple to follow.

image

I’m unsure how this would work across different depths though? Idea is depper you get, rarer ores are easier to find, etc.