Clone (Method): Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus
Don't; wait for geshi
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
 
(25 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<onlyinclude>{{Method
<onlyinclude>{{Method
|name                = Clone
|name                = Clone
|returns              = [[Instance]] ''object''
|returns              = {{type|instance=Instance}}
|description          = Returns a copy of this Object and all its children. The copy's Parent is [[nil]].
|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}}.
|object              = Instance
|object              = Instance
}}</onlyinclude>
}}</onlyinclude>
Line 8: Line 8:
{{clear floats}}
{{clear floats}}


{{Example|<pre>
{{Example|<syntaxhighlight lang="lua">
local Model = game.Workspace.Model
local Model = 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>}}


==Objects with ''archivable = false'' cannot be cloned==
==Objects that cannot be cloned==
When [[archivable]] is set to ''false'' on an object, the game sees that as a temporary object. When a temporary object enters the game for the first time, it checks to see if it has ''archivable = true''.  If it does, it is inserted into the game like normal, but if not, it is removed from the game and doesn't even call [[ChildAdded]], [[ChildRemoving]], [[DescendantAdded]], or [[DescendantRemoving]].  None of its [[Script]]s, [[LocalScript]]s, or [[CoreScript]]s run either.  Here's an example of what would happen if you were to try to clone a non-archivable object.
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}}.


{{Example|<pre>local Part = Instance.new( "Part", Workspace )
This means that if Clone is called directly on a non-archivable object, it will return nil.
print( Part ) --> Part
Part.archivable = false
print( Part ) --> Part
local newPart = Part:clone()
print( newPart ) --> nil</pre>}}


== Limitations ==
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.
*Objects with [[archivable]] set to false are not cloned.
 
*These classes are known to be unclonable: [[Hat]], [[Workspace]], [[BadgeService]], [[DataModel]], [[PlayerGui]], [[Players]], [[StarterGui]]
{{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