Jump to content

Help with while loop. (SOLVED)


Recommended Posts

function Formula(prefab, u1, u2, xRange, yRange)
    local player = ThePlayer
    local playerPos = player:GetPosition()
    for x = -xRange, xRange do
     for y = -yRange, yRange do
        local z = math.abs(x-u1) + math.abs(y - u2)
        while z == 1 do 
        local spawnPos = Vector3(playerPos.x+x, playerPos.y, playerPos.z+z)
        SpawnPrefab(prefab).Transform:SetPosition(spawnPos:Get())
   end
  end
 end
end

So, I'm trying to make a square of sandspikes with this command, but whenever I use it, it goes on infinitely and crashes.

I've been using it like this: Formula("sandspike", 1, 1, 1, 1)

 

Sorry if this is insanely obvious, I've never used while loops lol.

What is changing the Z value?  The while loop repeats until something changes Z to not longer equal one and I don't see anything changing that.

idk lua so there may be some language thing I'm missing where this is not the correct answer, but in the languages I do use the while loop must include an iteration or it will be infinite (or never proc depending)

Also - do you actually need a while loop here?  The do x and do y are iterating over your position, what is the while loop repeating for?

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...