RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a lootbox system with tables"
-- server script -- Create the loot table local lootTable = { {name = "Sword", chance = 0.2}, {name = "Shield", chance = 0.3}, {name = "Potion", chance = 0.5}, } -- Function to generate a random item from the loot table local function getRandomItem() local totalChance = 0 for _, item in pairs(lootTable) do totalChance = totalChance + item.chance end local randomNumber = math.random(0, totalChance) local currentChance = 0 for _, item in pairs(lootTable) do currentChance = currentChance + item.chance if randomNumber <= currentChance then return item.name end end end -- Function to give the player a random item from the loot table local function giveLoot(player) local itemName = getRandomItem() local item = game.ReplicatedStorage[itemName]:Clone() item.Parent = player.Backpack end -- Connect to the lootbox opened event game.ReplicatedStorage.LootBoxOpened.OnServerEvent:Connect(giveLoot)