Clone (Method): Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Camoy
au contrare, some services CAN be cloned.
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
 
(44 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<onlyinclude>{{Method
<onlyinclude>{{Method
|name                = Clone
|name                = Clone
|arguments            =  
|returns              = {{type|instance=Instance}}
|returns              = [[Instance]] ''object''
|description          = Returns a clone of the object and its children, unless its [[Archivable (Property)|Archivable]] property is false. The clone will have the same properties as the original object and the same descendants (except those with an [[Archivable (Property)|Archivable]] property set to false). The clone's [[Parent (Property)|Parent]] will be {{nil}}.
|description          = Returns a copy of this Object and all its children. The copy's Parent is [[nil]].
|object              = Instance
|object              = Instance
}}</onlyinclude>
}}</onlyinclude>
Line 9: Line 8:
{{clear floats}}
{{clear floats}}


{{Example|
{{Example|<syntaxhighlight lang="lua">
<pre>
local Model = Workspace.Model
local Model = game.Workspace.Model
while true do
while true do
     local ModelCopy = Model:Clone()
     local ModelCopy = Model:Clone()
     ModelCopy.Parent = game.Workspace
     ModelCopy.Parent = Workspace
     wait(300)
     wait(300)
end
end
</pre>
</syntaxhighlight>}}
Some services cannot be cloned.
<pre>
local new_workspace = Workspace:clone() --> Unknown object class "Workspace" while reading XML</pre>
}}


== Limitations ==
==Objects that cannot be cloned==
*Objects with [[archivable]] set to false are not cloned.
When [[Archivable (Property)|Archivable]] is set to {{false}} on an object, the game sees that as a temporary object that should not be involved in saving, nor cloning. When Clone creates the clone of an object and its descendants, any objects with the Archivable property set to {{false}} will not be included in the clone. For example, if a {{type|instance=Model}} contains a {{type|instance=Part}} whose Archivable property is set to {{false}}, when the {{type|instance=Model}} is cloned, the clone {{type|instance=Model}} will not contain a clone of the {{type|instance=Part}}.
*These classes are known to be unclonable: [[Hat]], [[Workspace]]
 
This means that if Clone is called directly on a non-archivable object, it will return nil.
 
Several classes of objects, such as {{type|instance=Hat|Hats}}, the {{type|instance=DataModel}} and [[service]]s can never be cloned, and behave in the same way as a non-archivable object.
 
{{Example|
This code demonstrates how you can prevent an object from being cloned using the Archivable property:
<syntaxhighlight lang="lua">local Part = Instance.new('Part', Workspace)
print(Part) --> Part
Part.Archivable = false
print(Part) --> Part
local newPart = Part:Clone()
print(newPart) --> nil</syntaxhighlight>}}


[[Category:Methods]]
[[Category:Methods]]

Latest revision as of 06:19, 27 April 2023

Clone( )
Returns Instance
Description: Returns a clone of the object and its children, unless its Archivable property is false. The clone will have the same properties as the original object and the same descendants (except those with an Archivable property set to false). The clone's Parent will be nil.
Member of: Instance


Example
local Model = Workspace.Model
while true do
    local ModelCopy = Model:Clone()
    ModelCopy.Parent = Workspace
    wait(300)
end


Objects that cannot be cloned

When Archivable is set to false on an object, the game sees that as a temporary object that should not be involved in saving, nor cloning. When Clone creates the clone of an object and its descendants, any objects with the Archivable property set to false will not be included in the clone. For example, if a Model contains a Part whose Archivable property is set to false, when the Model is cloned, the clone Model will not contain a clone of the Part.

This means that if Clone is called directly on a non-archivable object, it will return nil.

Several classes of objects, such as Hats, the DataModel and services can never be cloned, and behave in the same way as a non-archivable object.

Example

This code demonstrates how you can prevent an object from being cloned using the Archivable property:

local Part = Instance.new('Part', Workspace)
print(Part) --> Part
Part.Archivable = false
print(Part) --> Part
local newPart = Part:Clone()
print(newPart) --> nil