Ordering GetChildren() for cloning

Below is the code I use to fill the inventory with tools for different abilities. (Local script, starterplayerscripts)

-- Give user tools from specific persona
	local pTools = game.ReplicatedStorage.PersonaMoveTriggers:FindFirstChild(persona):GetChildren()
	for i, tool in pairs(pTools) do
		tool.Parent = game.Players.LocalPlayer.Backpack
	end

I do it this way so I have a system of dictionaries (above this code segment) set up with all the information, making a “hub” for all the data so I can change it if I have to.

This all works: the problem is that I want the moves to be in a certain order. I checked the folder with the tools where it is being cloned from, and the order is always the same, so it seems like GetChildren() is giving them in a random order? How can I fix this so that it is the same order every time?

You would need to specify the order yourself
(either in a dictionary/array or using attributes)
then sort them

local itemOrder = {"Item1", "Item2", "Item3"}

table.sort(pTools, function(a, b)
	return table.find(itemOrder, a.Name) < table.find(itemOrder, b.Name)
end)

for _, tool in pairs(pTools) do
	tool.Parent = game.Players.LocalPlayer.Backpack
end

i suppose you could also use an iterator function but to be fair dunno how you’d do that

The items in the folder are being found in the order they were placed there, regardless of how they appear in the studio or the order in the folder. It’s a bit odd, but that’s what it does. If you pull an item out, place it in terrain, and then put it back in the folder, your pick order will change.

I like to take the whole “folder” and drop it into terrain, then put it back. That fixes both the order and the name order.

you could go by name number like this:

local folder = workspace:WaitForChild("Folder")

for i, _ in ipairs(folder:GetChildren()) do
    local itemName = "item" .. string.format("%02d", i)
    local item = folder:FindFirstChild(itemName)
    if item then
        print(item.Name)
    end
end
-- item01, item02, item03 exc

or by defined name order like this

local folder = workspace:WaitForChild("Folder")
local itemOrder = {"Part", "Dart", "Tart", "Bart", "Fart"}

for i = 1, #itemOrder do
    local itemName = itemOrder[i]
    for _, item in pairs(folder:GetChildren()) do
        if item.Name == itemName then
            print(item.Name)
        end
    end
end
local folder = workspace:WaitForChild("Folder")
local itemOrder = {"Part", "Dart", "Tart", "Bart", "Fart"}

for i, v in itemOrder do
	local FoundTool = folder:FindFirstChild(v)
	if FoundTool then
		local ClonedTool = FoundTool:Clone()
		print(ClonedTool)
		ClonedTool.Parent = Player.Backpack
	end
end

btw, i dont recommend putting the tools folder in workspace, as tools have physical body “Handle” (unless u disabled that) and it might fall down somewhere to void causing it to disappear in explorer

1 Like

Ya, I meant terrain… Never had a problem with that. Not sure if i’ve ever done that with tools however.
I was just pointing out how the Studio isn’t showing the pull order as you see it in the folder.

2 Likes