Finding the smallest of Values in a Table

I want to find out the smallest magnitude between let’s say point (point)
and 2 other positions, indexed first and second in this sample :

x = {

[1] = Vector3.new(2,2,2) ;
[2]  = Vector3.new(1,1,1) ;

    }

point = Vector3.new(2,2,5)--going to compare magnitudes of other positions with this one
for i = 1 ,#x  do
	ss = {}
	table.insert(ss,((x[i]-point).Magnitude)) 
  print(table.unpack(ss)-------> prints 2 magnitudes
    for i = 1 , #ss do
	 f =  math.min(  ss[i],ss[i+1] )---trying to find the smallest number from 2 magnitudes
	print(f)----> bad argument #2 (number expected, got nil)
end 
       end 

Does any method exist to find the smallest number in a table containing numbers ?
If so, then some insight on this would be appreciated. (Not to compare values in a table using operators, but to find the minimum value)

Please excuse the lack of descriptive variables.

math.min accepts a tuple, so you can unpack a table into your function call and it’ll return the smallest value.

local points = {
	Vector3.new(1, 1, 1),
	Vector3.new(2, 2, 2)
}

local point = Vector3.new(2, 2, 5)

local distances = {}

for i = 1, #points do
	distances[i] = (points[i] - point).magnitude
end

return math.min(unpack(distances)) -- will return the smallest distance in the table.
10 Likes