Can someone please teach me how to use Clone() properly or at least how to copy? You can type it down
How does one use clone/copy? How does this instance work?
3 answers
Clone() has many functions that can be used for more info look here:https://developer.roblox.com/en-us/api-reference/function/Instance/Clone
To start we need to first make a clone of an object for example:
local Cpart = game.Workspace.Part:Clone()
Next we need to provide some fields like this:
local Cpart = game.Workspace.Part:Clone()
Cpart.Name = "Name_here"
Cpart.Parent = game.Workspace
Cpart.BrickColor = BrickColor.new("Really red")
As you can see .Name and .Parent are always available for use when using Clone(). Those are some of the basic uses of Clone() but the Clone() function is used in almost any game. Good Luck :)
It's better to make the cloned object a variable.
Here's how you do it:
local clone = game.Workspace.Part:Clone()
clone.Parent = game.Workspace
#Introduction:#
Clone is a very useful thing when it comes to creating things. Clone can reduce huge amounts of code and can be resource friendly to Scripts and LocalScripts.
#How is Clone script friendly?#
Well, a very good example would be an inventory system. Chances are, you iterate through a list of items and create a visual for it into a GUI then allow access to edit items, use items and such. But, when creating the slots / inventory items, it's better to clone. Why? Let's think about it. Instance.new() creates a instance with default properties that ROBLOX provides, but chances are you don't want those visual and you might want more things such as TextLabels to represent the amount or ImageLabels to show icons. It would be better to create a template, then clone it since it's just so much more easier, efficient, and resource friendly.
#How does Clone work?#
Well, it's basically in the name. To "Clone" something you need an original copy. So first you would create something you would like to clone, say a part. Next, you would store the part somewhere. The most common places would be the ReplicatedStorage or the ServerStorage. Next, you would simply reference the clone as a variable and then parent it to workspace!
Sample script:
local ToBeCloned = game.ReplicatedStorage.Part
local ClonedCopy = ToBeCloned:Clone()
ClonedCopy.Parent = workspace
now obviously after you clone the object you can tinker the properties and etc.
#Conclusion# Clone is useful and makes coding so much more quick and efficient.
#Sources:# https://developer.roblox.com/en-us/
This discussion is preserved as it was. New answers, comments, and votes are closed.
