local Item = script.Parent.Item local Price = script.Parent.Price local Currency = script.Parent.Currency script.Parent.ClickDetector.MouseClick:Connect(function(player) local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value) local ItemName = game.ReplicatedStorage.LootCrate[math.random] if CurrencyName.Value >=Price.Value then CurrencyName.Value = CurrencyName.Value - Price.Value ItemName:Clone().Parent = player.StarterGear ItemName:Clone().Parent = player.Backpack -- dont change anything here end end)
This is the script that I am using to do this. It is supposed to be like a loot-box that you can buy, and it chooses a random tool from the folder in Replicated Storage. The folder is named "Lootcrate"
I added comments. Let me know if you need any more explanation or if the code doesn't work.
local Item = script.Parent.Item local Price = script.Parent.Price local Currency = script.Parent.Currency function getRandomItem(location) --warn that the location they specified has nothing in it so we can't get a random item. if #location:GetChildren() == 0 then warn("there are no items in the location ".. location:GetFullName().."please try another location") return --return nothing end local itemTable = location:GetChildren() --table containing all the items inside the specified location local randomPosition = math.random(1,#itemTable) --picks a random position inside the table local randomItem = itemTable[randomPosition] --gets the item at the table's random positoin return randomItem --returns the item specidied end script.Parent.ClickDetector.MouseClick:Connect(function(player) local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value) print("the if statement condition is: ", CurrencyName.Value >= Price.Value) if CurrencyName.Value >= Price.Value then --cals the getRandomItem() function to get an item inside the location `game.ReplicatedStorage.LootCrate` local randomItem = getRandomItem(game.ReplicatedStorage.LootCrate) --keep in mind here you're getting ONE item and cloning it twice - once to startergear, once to backpack --if you want to do TWO different items, call the getRandomItem() function again. randomItem:Clone().Parent = player.StarterGear randomItem:Clone().Parent = player.Backpack -- dont change anything here CurrencyName.Value = CurrencyName.Value - Price.Value end end)