Skip to content

Documentation: Clone

Egiziano edited this page Jan 7, 2023 · 1 revision

Functions:

CloneAndParent

Summary: This function clones an instance Object and reparents the cloned instance to a given parent Parent, or to the same parent as the original object if no parent is specified. The number of clones to create can be specified with the optional argument Clones.

Syntax: CloneAndParent(Object: Instance, Parent: Instance?, Clones: number?) → (Instance | {Instance})

Parameters:
  • Object: The instance to be cloned.

  • Parent (Optional): The parent to reparent the cloned instance to. If not specified, the cloned instance will be parented to the same parent as the original object.

  • Clones (Optional): The number of clones to create. If not specified, the function will create 1 clone.

Retruns:
  • Instance|table: If Clones is 1, the function returns the single cloned instance. If Clones is greater than 1, the function returns an array of the cloned instances.

Code Example:
local Original = game.Workspace.Part
local Clones = Clone.CloneAndParent(Original, game.Workspace, 3)

for _, Clone in ipairs(Clones) do
	print(Clone.Name)
end

CloneWithoutChildren

Summary: This function creates a copy of an instance, but with no children and parents the copy to the original source or to the given parent.

Syntax: CloneWithoutChildren(Object: Instance, Parent: Instance?) → (Instance | {Instance})

Parameters:
  • Object: The instance to be cloned.

  • Parent (Optional): The parent of the cloned instance. If not specified, the parent of Object is used.

Retruns:
  • Instance: The cloned instance.

Code Example:
local Model = workspace.Model
local ClonedModel = Clone.CloneWithoutChildren(Model)
local ClonedModel_2 = Clone.CloneWithoutChildren(Model, game.ReplicatedStorage)

CloneWithProperties

Summary: Creates a clone of an object and applies the given properties to each of the clones.

Syntax: CloneWithProperties(Object: Instance, Properties: {[string]: any}, Clones: number?) → (Instance | {Instance})

Parameters:
  • Object: The object to be cloned.

  • Properties: A table of properties (property-value pairs) to be applied to the clones.

  • Clones (Optional): The number of clones to create. If not provided, a default value of 1 will be used.

Retruns:
  • Instance|table: If Clones is greater than 1, the function returns a table of the cloned objects. Otherwise, it returns the cloned object.

Code Example:
local OriginalObject = script.Parent
local Properties = {
	Parent = game.Workspace,
	Name = "New Name"
}
local Clones = Clone.CloneWithProperties(OriginalObject, Properties, 3)

CloneToMultiple

Summary: Creates a number of clones of the given object, and assigns each clone as a child of one of the instances in the given array.

Syntax: CloneToMultiple(Object: Instance, To: {Instance}, ClonesForEachInstance: number?) → ()

Parameters:
  • Object: The object to be cloned.

  • To: An array of instances to which the clones will be parented.

  • ClonesForEachInstance (Optional): The number of clones that will be created for each instance in the To array. This argument is optional, and if not provided, a default value of 1 will be used.

Retruns:
  • table: An array containing all of the newly created clones.

Code Example:
local Players = game:GetService("Players"):GetPlayers()
local Stats = game:GetService("ServerStorage").Stats
local Clones = Clone.CloneToMultiple(Stats, Players, 2)

--| The above code will create two clones of Stats and assign each clone as a child of every player. The Clones array will contain all of made clones.