Search code examples
lualua-table

If condition is met start function again [LUA]


I have a simple function to fill an array with random numbers

local function fillArray()
    local rand = math.random( 1,8 )
    if has_value(weaponOrder, rand) then
        -- I WANT TO RESTART FUNCTION HERE
        return false
    end
    return rand
end
for i = 1,8 do
    order[i] = fillArray()
end

I want if the condition is true (has_value is another function to check if the number exists again) to start the function again and return another result.


Solution

  • Use repeat-until statement.

    local function fillArray()
      local rand
      repeat
        rand = math.random( 1,8 )
      until not has_value(weaponOrder, rand)
      return rand
    end