Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this clone more than 50 times?

Asked by 9 years ago
for i = 1,50 do
    wait()
    clone = script.Parent:Clone()
    clone.Parent = workspace
    clone.Position = Vector3.new(-3,5,math.random(-209,-201))
    clone.Rotation = Vector3.new(math.random(0,180),0,-0)
end

I thought i = 1,50 do means it will run 50 times, but it clones over 2,000 times. (Not even exaggerating i selected all the parts and it said 2000+).

GIF: http://gyazo.com/62785aacac6da230ba9147f8bf5a5108

End: http://gyazo.com/21f388ffeb7ada24afe902bda960ccb1

BrickCount(1.8K): http://gyazo.com/a3659c4e4985095a57ec36ad0c67fe8d

2 answers

Log in to vote
1
Answered by 9 years ago

The reason it's cloning over 50 times is because you're cloning the scripts parent, which will house a clone of the script and will run that 50 times too.

To fix this, you need to disable the script inside the clone in your loop by doing clone[script.Name].Disabled = true, which will disable the script inside the clone. You could also destroy the clone's script, which I personally believe would be the better option.

Overall, the final code will look like this:

for i = 1,50 do
    wait()
    clone = script.Parent:Clone()
    clone[script.Name]:Destroy()
    clone.Parent = workspace
    clone.Position = Vector3.new(-3,5,math.random(-209,-201))
    clone.Rotation = Vector3.new(math.random(0,180),0,-0)
end

EDIT: You could also set the Archivable property of the script to false as suggested by duckwit as the script is then ignored when calling Clone on the part.

0
You can also set the script's Archivable property to false and then it won't be copied with the clone() method. duckwit 1404 — 9y
0
Oh, I see. I've never really used Archivable for anything. Thanks for the note. Spongocardo 1991 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

Use a repeat + variable. Here:

Parts = 0
repeat
    wait()
    local Clone = script.Parent:Clone()
    Clone.Parent = game.Workspace
    Clone.Position = Vector3.new(-3, 5, math.random(-209, -201))
    Clone.Rotation = Vector3.new(math.random(0, 180), 0, 0)
until Parts = 50

Answer this question