Tryna randomly generate ores w tables

tryna make scripts for a model that generates ores based on rarity every time the preexisting ore is broken. for my buddy’s game. i made the mistake of saying i’d handle the scripting on this one.

it keeps printing “nil” every 3 seconds and im pretty sure i have no damn clue what im doing :-p

-- PLACEHOLDER - no payout - no rarity
-- copper - cheap payout - common
-- coal - medium payout - common
-- bronze - high payout - common
-- lead - medium payout - uncommon
-- iron - high payout - uncommon
-- obsidian - cheap payout - very uncommon
-- gold - high payout - rare
-- diamond - very high payout - very rare

local OreModel = script.Parent
local Ores = OreModel.Ores

local Rarity = {
	["Very Rare"] = 2,
	["Rare"]= 5,
	["Very Uncommon"] = 18,
	["Uncommon"] = 25,
	["Common"] = 50
}

local OreRarity = {
	["Very Rare"] = {
		Ores.Diamond
	},
	["Rare"]= {
		Ores.Gold
	},
	["Very Uncommon"] = {
		Ores.Obsidian
	},
	["Uncommon"] = {
		Ores.Iron,
		Ores.Lead
	},
	["Common"] = {
		Ores.Bronze,
		Ores.Coal,
		Ores.Copper
	}
}

while task.wait(3) do
	local ChosenOre = OreRarity[math.random(1,100)]
	print(ChosenOre)
end
1 Like

Hello, I can see why your script is printing nil from this line. From this, you’re trying to reference the OreRarity table with a random number, which does not have any number indices, therefore returning nil.

For fixes, I’m not too sure on what you’re exactly trying to do, but hopefully this post will help.

1 Like

I’m just trying to print what ore was chosen from the table before moving forward. I have absolutely no clue what your post means :')

I think you should learn scripting before trying to script something that requires a bit of knowledge on tables…

1 Like

I know a lot of certain areas of scripting but I am self taught and do not know lots of terminology and lack knowledge in certain areas (especially when it comes to things related to math which is needed in this script :'/)

I spent a lot of time researching this subject and trying to figure out what I could but ultimately got stuck which is why I made this post. I don’t think this response was very constructive or appropriate.

Here’s a fix that I believe will help:

local Rarity = {
	["Very Rare"] = 2,
	["Rare"]= 5,
	["Very Uncommon"] = 18,
	["Uncommon"] = 25,
	["Common"] = 50
}

local OreRarity = {
	["Very Rare"] = {
		Ores.Diamond
	},
	["Rare"]= {
		Ores.Gold
	},
	["Very Uncommon"] = {
		Ores.Obsidian
	},
	["Uncommon"] = {
		Ores.Iron,
		Ores.Lead
	},
	["Common"] = {
		Ores.Bronze,
		Ores.Coal,
		Ores.Copper
	}
}

function chooseOre()
	local total = 0
	for i,v in pairs(Rarity) do
		total += v
	end

	local chosen = math.random()*total
	local current = 0
	local result
	for i,v in pairs(Rarity) do
		current += v
		if current >= chosen then
			result = i
			break
		end
	end

	local options = OreRarity[result]
	return options[math.random(1,#options)]
end

while task.wait(3) do
	local ChosenOre = chooseOre()
	print(ChosenOre)
end
1 Like

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