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

How to get this double table iteration to read/return properly?

Asked by
RoboFrog 400 Moderation Voter
9 years ago

As of now, I have a script (that may have been seen a couple times today) which spawns ores around a part, and when it does, it logs its x and z coords into a table.

But, to do so, it has to check over the table first to ensure that it isn't spawning too close to another ore by iterating over the table's values.

I have found that it will spawn the correct amount of ores in (mostly) random spots, and it also logs the coordinates correctly into the table. However, the issue persists to where it is unable to detect when it's too close, and still spawns over top (because of moveto) of other already placed ores.

I feel this may be because I can't get the function to iterate over just "x" or just "z" in coords. (as seen in line 08)

This has been bugging me all day, and if you need any more information or parts of the script to help, please don't hesitate to let me know. I appreciate any help that can be offered in this difficult predicament!

local mdist = 8;

local Coords = {
    {x=0,z=0}
}

function xcheck(xrand) 
    for index, value in pairs(Coords) do -- how do I get this to just look over the x in the table?
        if index + mdist <= xrand + mdist or index - mdist >= xrand - mdist then
            wait(.001);
        else
            print("False return x"); -- I never see this in the output.
            return false;
        end
    end
end

-- Repeat above but with z instead of x, it's just useless code to post here as added bulk.

------------------- NOT AS IMPORTANT BELOW -------------------

for i=1,limit do
        local boundsrandx = math.random(Bounds[1].x1, Bounds[1].x2);
        local boundsrandz = math.random(Bounds[2].z1, Bounds[2].z2);
        if (xcheck(boundsrandx) ~= false and zcheck(boundsrandz) ~= false)then -- This is what references the above functions.
            local cclone = copper:Clone();
            cclone.Parent = script.Parent;
            cclone:MoveTo(Vector3.new(boundsrandx,y,boundsrandz));
            local intclone = game.Lighting.Destroyed:Clone();
            intclone.Parent = cclone;
            cclone.Destroyed.listnumber.Value = currspawn;
            currspawn = currspawn + 1;
            table.insert(Coords, {x=cclone.ore.Position.X, z=cclone.ore.Position.Z});
            --print("Ore Spawned at ", "X ", boundsrandx, " and Z", boundsrandz);
            --print("There are ", #Coords, " entries in Coords.");
            --print("X ", Coords[i].x, " and ", Coords[i].z, " is the most recent entry.");
            wait(.5);
        else
            print("too close");
            wait(.5);
        end
    end

Answer this question