Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Any formula to make a drop chance on a table?

local Tools = {"Bazooka", "Gun", "Gun", "Gun", "Gun"}
print(Tools)

So here is like 4/1 chance to get a bazooka. Is there a better way to do this script?

1 answer

✓ Accepted answer
-- your list of items, I tend to have these in a seperate module
local items = {
	{Name = "Gun"},
	{Name = "Bazooka"}
}

--[[ 
	a loot table, it's wise to keep these seperate from your item list as it allows you to make
	multiple loot tables
	
	needs to be linear, greatest to least, and remember it's based on weight not percentages
--]]
local lootTable = {
	{Item = items[2], Weight = 100}, -- each one of these is an entry
	{Item = items[1], Weight = 20}
}

-- used in weight random number generation
local function returnSumOfWeight(lootTable)
	local sum = 0
	for _, entry in ipairs(lootTable) do
		sum = sum + entry.Weight
	end
	return sum
end

-- returns a random item from given lootTable
local function getRandomItem(lootTable)
	-- returns a random number based on total weight of given lootTable
	local randomNumber = math.random(returnSumOfWeight(lootTable))
	
	for _, entry in ipairs(lootTable) do
		if  randomNumber <= entry.Weight then
			return entry.Item
		else
			randomNumber = randomNumber - entry.Weight
		end
	end
end

-- just for testing, mess around with it how you like
for i = 1, 100 do
	local item = getRandomItem(lootTable)
	print(item.Name)
end

This should helpp you out just do it how it shows accept if helped

2 comments

0

That's such a long a** code ????

0

Lol But it works :D

This discussion is preserved as it was. New answers, comments, and votes are closed.