How to swap items in a folder around

I want to basically get 2 folders, and swap their contents around at the same time. Reason for this is I have a function that listens for when ChildAdded/ChildRemoved happens on these folders, and it can cause problems, because all the stuff from ‘FolderA’ goes into ‘FolderB’ and B still has its items, until a frame (or idk how long) before its items get moved over.

So here, all the items from ‘OldParentLocation’ get put into ‘NewFolderLocation’ while the contents of ‘NewFolderLocation’ is still there. So for a split second, ‘NewFolderLocation’ has both ‘OldParentLocation’ contents and its original contents

-- Get the stuff from the old parent and new parent
	local OldParentData = {}
	local NewParentData = {}
	
	for _, v in pairs(OldParentLocation:GetChildren()) do
		table.insert(OldParentData, v) 
	end
	
	for _, v in pairs(NewParentLocation:GetChildren()) do
		table.insert(NewParentData, v) 
	end
	
	-- Parent old to new and vice versa
	for _, v in pairs(OldParentData) do
		v.Parent = NewParentLocation
	end

	for _, v in pairs(NewParentData) do
		v.Parent = OldParentLocation
	end

For anyone curious as to what this is, it’s basically for an inventory system where you can drag and drop items around your inv, so when they drag an item I need to swap the items data around so the data folders correspond to what the UI is showing

Why don’t you just temporarily parent both to nil/random place, then reparent them to the new folder? That way they are only gone for a split second, then return at almost the exact same time.

EDIT: Or if you really wanted to, you could use coroutines to make it super speedy.

if i am understanding what you want, you just need to getchildren for both folders before you start the first loop

    local OldParentData = --folder1:GetChildren()
	local NewParentData = --folder2:GetChildren()
	
	for _, v in pairs(OldParentLocation) do
		v.Parent = folder2
	end
	
	for _, v in pairs(NewParentLocation) do
		v.Parent = folder1
	end