How to get the closest number that's less than another number?

Hello, I was wondering how I would find a specific number through a table. The number must be:

  1. Less than a specific number.
  2. Closest to this specific number.
    For example, If there are three numbers, 1, 3, 8, And the specific number is 7. The number that would be printed out is 3. Since it’s less than 7 and the closest to 7.
    I’m not really good at explaining, so this is the best I could do to my ability to explain. All help is appreciated!
local woah_table = {4,3,2,19}
local idk = {} -->in this table we will get the numbers that are lower than 7

for i,v in pairs(woah_table) do
if 7 >v then
table.insert(idk,v)
end 
 end 

local closest = idk[1]
local value = math.abs(closest-7)
for i,v in pairs(idk) do
if v > value  then closest = idk[i] value = v  end 
 end 

this is un tested script if something is wrong please correct me
edit: it works

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.