Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make it to where the script clones a random tool from a folder in Replicated Storage?

Asked by
Nump4d 5
5 years ago
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"

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

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)

0
thanks mate, your help is greatly appreciated B) Nump4d 5 — 5y
0
np. thanks for taking the time to respond back royaltoe 5144 — 5y
0
noice Tixabel 19 — 5y
Ad

Answer this question