local LootTable = {
{ item = "Sword", chance = 30 }, -- 30% chance of getting a Sword
{ item = "Shield", chance = 20 }, -- 20% chance of getting a Shield
{ item = "Potion", chance = 50 } -- 50% chance of getting a Potion
}
function chooseItem()
local totalChance = 0
for _, entry in ipairs(LootTable) do
totalChance = totalChance + entry.chance
end
local randomChance = math.random(1, totalChance)
local cumulativeChance = 0
for _, entry in ipairs(LootTable) do
cumulativeChance = cumulativeChance + entry.chance
if randomChance <= cumulativeChance then
return entry.item
end
end
end
-- Example usage: chooseItem() will return a random item based on the chances in the loot table.
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local function giveRandomItem()
local item = chooseItem()
local newItem = game.ReplicatedStorage.Items:FindFirstChild(item):Clone()
newItem.Parent = backpack
end
-- Example usage: Call giveRandomItem() to give the player a random item from the loot table.
How would i make chances lower than 1% bc math.random cannot generate that low
local lootTable = {
["Common"] = 0.5, -- 50%
["Mythic"] = 0.01 -- 1%, etc.
}
local chosen = math.random()
local totalPercentage = 0
for i, percentage in pairs(lootTable) do
totalPercentage += percentage
if chosen <= totalPercentage then
return i -- this one is picked
end
end
something like this should work you can reverse the keys and values you would have to times by 100