Cloning an Instance without cloning its children

I often find myself needing to clone an instance exactly without including children. I could temporarily remove the children or make them non-archivable, but this can inadvertently interact with other scripts and potentially mess with ChangeHistoryService when done from a plugin or the command bar.

Some use-cases:

  • Cloning multiple instances while maintaining instance references between them (e.g. ObjectValue.Value).
  • Cloning a part that has extra data (such as offset values) stored in it, without cloning unnecessary instances.

Here’s my current workaround. It works for most cases, but is extremely slow and impractical when cloning top-level instances with thousands of children:

local function CloneWithoutChildren(instance)
	local instance2 = instance:Clone()
	instance2:ClearAllChildren()
	return instance2
end

Another workaround could be to store property names in lists and clone the instance manually, but this is slower in some cases, and does not work on some instances including Scripts, MeshParts and Unions.

A solution to this problem could be implemented as a new :CloneWithoutChildren() method for instances.
Although unlikely, this may interfere with scripts that try to access children named “CloneWithoutChildren” (I personally always use :FindFirstChild for this reason)

local instance2 = instance:CloneWithoutChildren()

It could also be implemented as a new parameter for :Clone().
This would cut down on API bloat, but perhaps this parameter should be saved for a different feature (like ignoring .Archivable).

local instance2 = instance:Clone(true)
37 Likes