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

How can I use an if statement with an array iteration check? (advanced)

Asked by
RoboFrog 400 Moderation Voter
9 years ago

This is probably an advanced concept. As of now, this spawns a model into and onto a platform, then sets its x and z coordinates into a table. Everything is working great, but I want to require it to only spawn when the random x and z coordinates are mdist (6 blocks in this case) away.

So, I have marked the if line in question with a lot of hyphens, and it currently contains a mock "~=1" combo for placeholders sake.

The biggest issue I've encountered so far is that the iteration through a list will likely require a "for" loop, and I don't believe those work inside of a one line if statement. I'll use the 6 block comparison by requiring it to be greater than/less than +- mdist, but I can't get it to iterate properly though the table with the coordinates while inside of an if statement.

Any help is greatly appreciated, and thank you for your time!

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

local copper = game.Lighting.oretotal.Copper;

local currspawn = 1;

local mdist = script.MaxDistance.Value;
local limitnum = mdist * script.closeMultiplier.Value;
local xs = script.Parent.Size.X;
local zs = script.Parent.Size.Z;
local initial = script.Parent.Initrun.Value;

local waittime = game.Lighting.oretotal.respawn.Value; 
local limit;

local xhalf = xs/2;
local zhalf = zs/2;

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

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

local Coords = {
}

for i=1,limit do -- limit is a varying integer value
        local boundsrandx = math.random(Bounds[1].x1, Bounds[1].x2); -- sets random position within limits.
        local boundsrandz = math.random(Bounds[2].z1, Bounds[2].z2); -- see above
        if (boundsrandx ~= 1 and boundsrandz ~= 1)then -- this is the line in question ----------------
            local cclone = copper:Clone(); -- creates the model
            cclone.Parent = script.Parent; -- sets the model inside spawn platform
            cclone:MoveTo(Vector3.new(boundsrandx,y,boundsrandz)); -- moves it
            local intclone = game.Lighting.Destroyed:Clone(); -- clones a bool that contains an int
            intclone.Parent = cclone; -- puts bool and int in model
            cclone.Destroyed.listnumber.Value = currspawn; -- sets integer to spawn order
            currspawn = currspawn + 1; -- sets spawn limit up by one
            table.insert(Coords, {x=cclone.ore.Position.X, z=cclone.ore.Position.Z}); -- puts the object coords into the table
            wait(.5);
        else
            nil;
            wait(.5);
        end
    end

EDIT It seems to me that I could probably just feed the numbers through another function then return them for the check, but I'll still keep this up to find if there's another better way.

Answer this question