How would i go about selecting random items from the loottable of a crate? I already have the part of the script that selects the items, i just dont know how to add x amount of items based on its chance to unlock
Its quite simple, for this example I used an array with the possible drops (as strings) but you can change it to use whatever you use for the loot tables.
Heres a sample script.
local lootTable = {
"Fire Axe",
"Glock-17",
"AK-47",
"AR-15",
"AA-12",
"Saiga-12",
"Uzi",
"Sten Gun",
"Grease Gun",
"Glock-18",
"FN Five Seven"
}
local function pickDropFromLootTable (player:Player)
if not player then return end -- Make sure passed player exists
local backpack = player.Backpack -- Get the player's backpack
local randomDrop = lootTable[math.random(1, #lootTable)] -- Pick a drop from the lootTable randomly
-- Prints picked drop, change to whatever code you need
print("Random loot drop from lootTable is: ".. randomDrop)
end
-- Calls the function 10 times while waiting 1 second between each call
for i=1,10 do
task.wait(1)
pickDropFromLootTable(game:GetService("Players").usernameHere)
end
Edit the sample code however you need.
For an explanation of how the drop picker works, it just calls the lootTable and picks a specific array instance using the math.random function, with a minimum value of 1 and a maximum value dictated by the amount of keys in the array.
lootTable[math.random(1, #lootTable)]
lootTable is the Array, 1 is the minimum value, and the maximum value is dictated by the number of keys in the lootTable (#lootTable)
The way my game works, each crate item has a rarity, i want it to select items based on its rarity lets use this as an example:
chances = {
Crate_Name = {
Common = 60,
Uncommon = 20,
Rare = 15,
Epic = 3,
Legendary = 2
}
}
based on its chance to obtain, i want it to loop through the loottable (i have already made the script that creates the loottable) and select items based on its rarity, so it should be like this:
Common: 22 items
Uncommon = 12 items
Rare = 9 items
Epic: 4 items:
Legendary: 1 item
You can use a weighted table / chance function for this:
local module = function(items)
local total = 0
for _, item in pairs(items) do
total += item.chance
end
local value = math.random() * total
local currentSum = 0
for name, item in pairs(items) do
currentSum += item.chance
if value <= currentSum then
return name
end
end
end
return module
Just define the items when calling the module, items should be defined like:
local items = {
["Item"] = {chance = 5, other data here},
["Item2"] = {chance = 20, other data here},
}
This works! but what exactly is a weighted table/chance function?
A weighted table / chance system is basically an advanced version of math.random() used by most RNG games you see out there. It starts by adding all “chances” together into a total value, it then uses math.random() * total
which generates a value between (0, total). Lastly it loops through every item to see if the currentSum is bigger or equal to the chosen value (0, total), if it is not it adds that items chance to currentSum and continues, if it is then it returns the item. This way you can effectively create an rng system. You can also create an inversed version of this where the higher the chance number, the lower chance for it to be picked. For that you can simply add 1/item.chance
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.