Instance:Clone() not working correctky

Hi,
So I have the following code:

 for i = 1, 3, 1 do
newPart = script.Parent:Clone()
newPart.Parent = workspace
newPart.Position = script.Parent.Position
newPart.Anchored = true
newPart.BrickColor = BrickColor.random()
script.Parent.Position = Vector3.new(script.Parent.Position.X + 16, script.Parent.Position.Y, script.Parent.Position.Z)

end

and in theory it should put 4 squares next to each-other, but for some reason it does this:

Can I have some help?

Thanks in advance.

for i = 1, 3, 1 do
newPart = script.Parent:Clone()
newPart.Parent = workspace
newPart.Position = script.Parent.Position
newPart.Anchored = true
newPart.BrickColor = BrickColor.random()
script.Parent.CFrame = CFrame.new(script.Parent.Position.X + 16, script.Parent.Position.Y, script.Parent.Position.Z)

Using position can cause collisions to be a factor in positioning the object. CFrame should fix this.

Ditto to the above. The other point I would add is that I was advised to Parent the part only after all other settings had been defined.

When it clones 6 times, it’s likely because your cloning the script as well. Perhaps move the script out of our part and index the position of new part through workspace?

A simply workaround is by destroying the script when cloned.

newPart = script.Parent:Clone()
newPart.Script:Destroy()
1 Like


That happens.

After cloning please remove the script from cloned parts too.

for i = 1, 3, 1 do
newPart = script.Parent:Clone()
newPart:FindFirstChild("Script"):Destroy()
newPart.Parent = workspace
newPart.Position = script.Parent.Position
newPart.Anchored = true
newPart.BrickColor = BrickColor.random()
script.Parent.Position = Vector3.new(script.Parent.Position.X + 16, script.Parent.Position.Y, script.Parent.Position.Z)
end

Thats because when you clone script.Parent it also clones everything in it
EX:

PartWeAreCloning

script

everything there would get cloned. So in the cloned part you should delete new cloned script

here is the code you should use

 for i = 1, 3, 1 do
newPart = script.Parent:Clone()
newPart[script.Parent.Name]:Destroy()
newPart.Parent = workspace
newPart.Position = script.Parent.Position
newPart.Anchored = true
newPart.BrickColor = BrickColor.random()
script.Parent.Position = Vector3.new(script.Parent.Position.X + 16, script.Parent.Position.Y, script.Parent.Position.Z)
end