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

How do I clone the children of a folder?

Okay, so I am making a game. This game involves classes, and I have it set so when somebody chooses for an item to go into a certain class, those items are cloned to a folder inside that player (game.Players.LocalPlayer). But then when they go to play the game, the items are not being cloned into the player's backpack and I receive the following error; Players.Player1.PlayerGui.Play.TextButton.LocalScript:5: attempt to call method 'Clone' (a nil value) Here is my code;

local ClassOne = game.Players.LocalPlayer:WaitForChild("ClassOne")

script.Parent.MouseButton1Click:connect(function()
	local GetItems = ClassOne:GetChildren()  -- I need help here,
	GetItems:Clone().Parent = game.Players.LocalPlayer.Backpack -- and here.
end)

Any help would go a long way, and is always appreciated. Thanks.

2 answers

✓ Accepted answer

What you need to do here is loop through the children

The method :GetChildren() returns an array containing all the children in that Instance. Using a for loop, you can check what is inside this table

eg:

local tbl = {"a","b","c"}
for i,v in pairs(tbl) do
	print(i,v)
end
-- Returns:
-- 1 "a"
-- 2 "b"
-- 3 "c"

Now, for you, you want to go through the table and clone everything, which will be as follows:

local ClassOne = game.Players.LocalPlayer:WaitForChild("ClassOne")

script.Parent.MouseButton1Click:connect(function()
    local GetItems = ClassOne:GetChildren()  -- I need help here,
	for i,v in pairs(GetItems) do
	 v:Clone().Parent = game.Players.LocalPlayer.Backpack
	end
end)

And that should do it for you,

Good luck!

1 comment

0

Nice doing what I did

local player = game.Players.LocalPlayer
local ClassOne = player:WaitForChild("ClassOne")

script.Parent.MouseButton1Click:connect(function()
	if ClassOne then
		local GetItems = ClassOne:GetChildren()
		for i,v in pairs(GetItems) do --i = index, v = value; it loops through the children of ClassOne and for every item it clones it into the players backpack
			local clone = v:Clone()
			clone.Parent = player.Backpack
		end
	end
end)

Hope I helped!

1 comment

0

The reason why your answer isnt accepted is because you don't explain what you do. One in-line comment isnt going to clarify what everything does.

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