Loottable chance issue

This video shows how to make a random loottable but am I able to make the chance less than 1 something like 0.1 pr 0.01, etc

1 Like

Thats pretty simple, I made a little example that can be multi-purpose

local chanceTable = { --Out of 100%
	[1] = 24.5;
	[2] = 24.5;
	[3] = 24.5;
	[4] = 24.5;
	[5] = 1.9;
	[6] = 0.1;

function getRandomItem()
	local chance = math.random(0.1, 100)

	for i,v in pairs(chanceTable) do
		if chance > v then
			chance -= v
			return i

No but from the video example how he did it

They already did it for you. You just have to add on to the loottable

But will it work if I have a decimal chance for example: 0.1,0.01, 0.001, 0.0005

with decimals you’ll have to do a little extra math for it to work.
you just gotta multiply everything by 10 for each decimal place you have because math.random will only return integers

so what do I do in order to do the extra math

function GetRandomItem()
	local sum = 0
	for SwordName, Chance in pairs(loottable) do
		sum += Chance * 10 -- add a 0 for each decimal place
	local randomNumber = math.random(sum)
	for swordName, Chance in pairs(loottable) dp do
		if randomNumber <= Chance * 10 then -- add a 0 for each decimal place
			return swordName
			randomNumber -= Chance * 10 -- add a 0 for each deciaml place

how about if the chance is less than 0.1 like 0.01 then do i multiply by 100

if the lowest chance is 0.1 then you multiply all the chances by 10
if the lowest chance is 0.01 then you multiply all the chances by 100
if the lowest chance is 0.001 then you multiply all the chances by 1000 and so on

How would this work cause if the chosen chance is 50 then I multiply it by let’s say 1,1000 (1000)

math.random() only accepts int values (non-float values, aka no decimals). Instead, you’ll have to divide for that.


This will result in anything from .1 to 1.

However in your case, it might be better to just do:


Check to see if the result is 1. That will be a 1/1000, which is a 0.1% chance.

If I did this if statement would it count as a 1 in 1000

if math.random(1,1000) == 1000 then
Do something


Yes, though I would do == 1. It wouldn’t really matter in a single case, but it’s a good habit to check if 1, as 1 will never change, where as the max number can always change.

Wdym it can change? And wdym single case?

I mean that if you want to change the rarity, the only value you’d change is the second number. Where was the first will never change (most of the time). It’s a good habit so you don’t accidentally copy/paste it and it and wonder why it’s never true when it’s only 1,100 or something else. The rarity may also be dynamic. Point is, it’s safer to check if it equals the lowest value rather than the highest.

Everything is multiplied so all the chances will remain the same, sorta like how simplifying fractions work