Clone creates a copy of an object and all of its descendants, ignoring all objects that are not
Instance/Archivable|Archivable. The copy of the root object is returned by this function and its
Instance/Parent|Parent is set to nil.
If a reference property such as
ObjectValue/Value is set in a cloned object, the value of the copy’s property depends on original’s value:
- If a reference property refers to an object that was also cloned, an internal reference, the copy will refer to the copy.
- If a reference property refers to an object that was not cloned, an external reference, the same value is maintained in the copy.
This function is typically used to create models that can be regenerated. First, get a reference to the original object. Then, make a copy of the object and insert the copy by setting its
Instance/Parent|Parent to the
Workspace or one of its descendants. Finally, when it’s time to regenerate the model,
Instance/Destroy|Destroy the copy and clone a new one from the original like before.
This code first references an existing object in the
original variable. Then, it makes a copy of the object, sets the parent to that of the original, and finally moves the copy to (0, 50, 0).
-- Get a reference to an existing object local original = workspace.Model -- Create the model copy local copy = original:Clone() -- Parent the copy to the same parent as the original copy.Parent = original.Parent -- Move the copy so it's not overlapping the original copy:SetPrimaryPartCFrame(CFrame.new(0, 50, 0))