Hello, I am having a problem with a script and I am not entirely sure what is causing it.
Basically, I want this script to make a bunch of clones in somewhat random locations. Which it does perfectly for a little bit. Although, after a couple times through the loop, it starts to only clone parts of the model, particularly one part and the scripts, that's it.
It doesn't clone the rest of the model.
I cannot figure out what might be wrong.
function _M.clones(animal) while _M.start do wait(.5) --Problem area from here... for x= math.random(2),0,-1 do wait(.4) animal = animal:Clone() wait() animal.Parent = game.Workspace animal:MakeJoints() animal:MoveTo(Vector3.new(math.random(175), 250, math.random(225))) --To here... local Colors = animal:GetChildren() local color = BrickColor.new(math.random(),math.random(),math.random()) --Just here to randomize color. for i = 1, #Colors do if Colors[i].ClassName == "Part" and Colors[i].BrickColor.Name ~= "Pink" then Colors[i].BrickColor = color end end end end end
I would like to mention that this function is present in a Module script, not sure if that would change anything but I figured it is worth mentioning.
Let me know if you can find anything or at least give me an idea of what might going on.
Thanks!
AHA! Found the mistake.
Logic mistake on my end.
I was reassigning animal
to be the clone and then cloning the clone itself!
Remember walk through your code one step at a time guys!
Fixed for reference:
function _M.clones(animal) while _M.start do wait(.5) --Problem area from here... for x= math.random(2),0,-1 do wait(.4) --Created a new variable to assign the clones to. local clone = animal:Clone() wait() clone.Parent = game.Workspace clone:MakeJoints() clone:MoveTo(Vector3.new(math.random(175), 250, math.random(225))) --To here... local Colors = clone:GetChildren() local color = BrickColor.new(math.random(),math.random(),math.random()) --Just here to randomize color. for i = 1, #Colors do if Colors[i].ClassName == "Part" and Colors[i].BrickColor.Name ~= "Pink" then Colors[i].BrickColor = color end end end end end
The script worked just fine for me, after 5 minutes it still worked well, but otherwise it could be the for x statement at line 5, hope I helped.