Help with chance system

Hey, I’m trying to make a chance system but whenever something is picked, it prints everything below it aswell:

function GetRandomOre()
	local sum = 0
	for orename, chance in pairs(oretable) do
		sum += chance
	end
	local randomnumber = math.random(sum)
	for orename, chance in pairs(oretable) do
		if randomnumber <= chance then
			print(orename)
		end
		randomnumber -= chance
	end
end

the oretable:

return {
	["Common"] = 60,
	["Uncommon"] = 30,
	["Rare"] = 10,
}

So if I get Uncommon for example, it prints this:

image

Once you get your object selected, break the for loop.

Like this?

function GetRandomOre()
	local sum = 0
	for orename, chance in pairs(oretable) do
		sum += chance
	end
	local randomnumber = math.random(sum)
	for orename, chance in pairs(oretable) do
		if randomnumber <= chance then
			print(orename)
		end
		randomnumber -= chance
		return orename
	end
end

Wait, I misunderstood, you want to print everything with a greater chance, correct?

I just want to print the ore selected, each ore has a different chance and everytime the function GetRandomOre is running, I want to get only one result. So in this case Common has a 60% chance of being chosen, Uncommon 30% and Rare 10%

Alright, so if you wanted to JUST print the object it selected, then once it selects and object, return the object it selected. If you don’t want it to stop all the code in the function, then just type “break”

I don’t quite understand, can you maybe edit the script?

function GetRandomOre()
	local sum = 0
	for orename, chance in pairs(oretable) do
		sum += chance
	end
	local randomnumber = math.random(sum)
	for orename, chance in pairs(oretable) do
		if randomnumber <= chance then
			return orename
		end
		randomnumber -= chance
	end
end

Is this what you are looking for?

to print the ore, do print(GetRandomOre())

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