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

How can I fix this cloning loop? (only spawns 1 model)

Asked by
RoboFrog 400 Moderation Voter
8 years ago

I'm still a bit rusty after finally coming back to Lua, so I expect it's an easy fix and an obvious error.

As of now, this script will copy the specified model, move it to the correct parent, and then just move that one model around randomly within 1 block of a random point instead of randomly around the whole area. I assume that's because it takes the same random number first iterated, but also have a hunch I may just need to use seeds.

But, the largest issue here is that it only copies one model and moves it for the iterated amount of times. I'm pretty sure it's because I used an iteration without renewing it, but I'm not sure.

I just copied the relevant part of the code, so if there are any missing variables, just let me know and I can add them. Thank you!

local x = script.Parent.Position.X;
local y = script.Parent.Position.Y - 5;
local z = script.Parent.Position.Z;

local copper = game.Lighting.oretotal.Copper;
local cclone = copper:Clone();

local Bounds = {
    {x1=x-xhalf,x2=x+xhalf},
    {z1=z-zhalf,z2=z+zhalf}
}

local boundsrandx = math.random(Bounds[1].x1, Bounds[1].x2);
local boundsrandz = math.random(Bounds[2].z1, Bounds[2].z2);

for i=1,limit do -- limit is a numbered variable
        cclone:MoveTo(Vector3.new(boundsrandx,y,boundsrandz))
        cclone.Parent = script.Parent;
        print("X ", boundsrandx, " and Z", boundsrandz);
        print("Ore Spawned");
        wait(.5)
    end

1 answer

Log in to vote
1
Answered by 8 years ago

I know why, I had this problem with guns being put into one pack. Okay, so here is you problem, you have the object clones once, and then expect it to be cloned every time you call it. Instead of having

local cclone = copper:Clone()

at line six, have it in the loop, between line 16 and 17, so it becomes the new line 17, and it is cloned > >every time it iterates through. And fyi, this isn't C, C++ or C#, you don't have to have ";" at the end of > >every line. But good practice if you are learning any of those languages.

for i=1,limit do -- limit is a numbered variable
    local cclone = copper:Clone() --remove the cclone at line six, and this one clones every time it iterates.
        cclone:MoveTo(Vector3.new(boundsrandx,y,boundsrandz))
        cclone.Parent = script.Parent;
        print("X ", boundsrandx, " and Z", boundsrandz);
        print("Ore Spawned");
        wait(.5)
    end
0
Wow, that is actually correct. It's hard to believe that the position of that one line made all the difference. And I actually code in C# most of the time, so I guess it's just habit xD. Thank you for the help! RoboFrog 400 — 8y
0
It also fixed the random number generation when I moved the math randoms into the iteration, two birds with one stone! RoboFrog 400 — 8y
0
Using semicolons overall is good practice for coding languages. When I code I use them, but not when I answer questions on here because it may confuse people. I think they might have the slightest performance rate as well, because Roblox's compiler IS C++, so I think all the lines are converted to semicolons when it is compiled, anyways. Goulstem 8144 — 8y
0
It was my pleasure. And as for Goulstem, I had no idea it was a C++ compiler, but I guess so since it is a PC game, so I suppose I should do it my self now... rollercoaster57 65 — 8y
Ad

Answer this question