Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

3 comments

0

thanks mate, your help is greatly appreciated B)

0

np. thanks for taking the time to respond back

0

noice

This discussion is preserved as it was. New answers, comments, and votes are closed.