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
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.
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.
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