Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does it change after a couple runs?[Answered]

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!

1 comment

0

Think I just spotted it, logical error...I will post the fix after I test

2 answers

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

2 comments

1

Oh, and good luck on raining cats and dogs ;P, i starred and thumbs upped it

0

Thanks!

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.

1 comment

0

I had some of the clones destroyed, thats why it eventually only start cloning the existing bits. I posted the fix. Thanks for the answer though!

This discussion is preserved as it was. New answers, comments, and votes are closed.