RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a lootbox system with tables"
-- server script -- Create a table to store lootbox data local lootboxes = {} -- Create a function to generate lootboxes local function generateLootbox() -- Generate a random position local x = math.random(-100, 100) local z = math.random(-100, 100) local pos = Vector3.new(x, 50, z) -- Create a lootbox local lootbox = Instance.new("Part") lootbox.Parent = game.Workspace lootbox.Position = pos lootbox.Size = Vector3.new(2, 2, 2) -- Add the lootbox to the lootboxes table table.insert(lootboxes, lootbox) end -- Generate the lootboxes for i = 1, 10 do generateLootbox() end -- Create a function to open the lootboxes local function openLootbox(player, lootbox) -- Check if the lootbox is valid if not lootbox then return end -- Remove the lootbox from the lootboxes table for i, v in ipairs(lootboxes) do if v == lootbox then table.remove(lootboxes, i) break end end -- Destroy the lootbox lootbox:Destroy() -- Give the player a random reward local rewards = { "Coin", "Gem", "Hat", "Shirt", "Pants" } local reward = rewards[math.random(#rewards)] player.leaderstats[reward].Value = player.leaderstats[reward].Value + 1 end -- Create a function to handle lootbox touches local function onLootboxTouched(otherPart) -- Check if the part is a humanoid local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid") if not humanoid then return end -- Get the player local player = game.Players:GetPlayerFromCharacter(humanoid.Parent) if not player then return end -- Open the lootbox openLootbox(player, script.Parent) end -- Connect the touched event script.Parent.Touched:Connect(onLootboxTouched)