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

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

Asked by
Necrorave 560 Moderation Voter
9 years ago

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!

0
Think I just spotted it, logical error...I will post the fix after I test Necrorave 560 — 9y

2 answers

Log in to vote
0
Answered by
Necrorave 560 Moderation Voter
9 years ago

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

1
Oh, and good luck on raining cats and dogs ;P, i starred and thumbs upped it connor12260311 383 — 9y
0
Thanks! Necrorave 560 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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.

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! Necrorave 560 — 9y

Answer this question