# Find magnitude and part in table?

I am kinda stuck logically. You see, basically what happens is, the function runs through a table of parts, and then it checks which one has the lowest magnitude, then returns it.

But, I am attempting to find WHICH part has the lowest magnitude, and to do that I assumed I need to find the lowest magnitude first. I have no idea to how return the part that has the lowest magnitude. Does anyone have any clue?

``````local function distanceCheck(otherPart)
local distances = {}
for i, v in pairs(otherPart) do
local check = (HRP.Position - v.Position).Magnitude
distances[i] = check
end
return math.min(unpack(distances))
end
``````
1 Like

I would look @Exeplexâ€™s post. And use `math.huge` for the highest magnitude instead of 9999999.

All I could think of is adding a second for loop below which loops through all the distances and keeps track of the smallest one. unpack wonâ€™t work since it does indxes and values.

``````local maxDistance = math.huge
for i,v in pairs(distances) do
if v < maxDistance then
maxDistance = v
end
end
return table.find(distances,maxDistance)
``````
``````local function distanceCheck(otherPart)
local lowestmagnitude = 9999999 - Some insanely high number for the first check to pass
local closestpart = nil

for i, v in pairs(otherPart) do
local check = (HRP.Position - v.Position).Magnitude
if check < lowestmagnitude then -- If the magnitude is lower than the previous lowestmagnitude
lowestmagnitude = check -- Set the new low
closestpart = v -- Set the part that is closest
end
end
return closestpart, lowestmagnitude
end

local closestpart, lowestmagnitude = distanceCheck()
``````

You just have to set an insanely high value for the first part to pass as the â€ślowest magnitudeâ€ť, then every other part will be checked against it. When a part has a lower magnitude, change the variables to the lowest magnitude and what part is closest. In the end, you can return them.

Edit: You donâ€™t have to return the lowest magnitude, itâ€™s just an extra in case you need it. You can simply just return the part into one variable

3 Likes

Thanks bro!
I am confused, why use a high number for the first check, so it can get all parts available in the table?

Lets say you have 3 parts. Their magnitudes are: 5, 10, 15

If you set the lowestmagnitude variable to 0. None of those parts will be considered closest, because their magnitudes are higher than 0.

If you set lowestmagnitude to 9999. 5 will be the closest part. Because it is lower than 9999.

Because 15 is lower than 9999, 10 is lower than 15, 5 is lower than 10. Also in case you only have 1 part in the array, that part will be the closest

1 Like

You can actually find the lowest magnitude without using a very large number plus a tiny optimization with allocated table memory creation if you use `table.create` for an array.

The original solution is however better because of the maximum distance that you can set. I below have some alternatives to use which will do the same thing of course.

No large number alternative
``````local function pairsFind(t, value)
for i, v in pairs(t) do
if v == value then
return i
end
end
end

local function partWithLowestMagnitude(parts, compareTo)
local t = table.create(#parts)
for _, part in ipairs(parts) do
t[part] = (part.Position - compareTo.Position).Magnitude
end
local lowest = math.min(unpack(t))
return pairsFind(t, lowest), lowest
end

local part, magnitude = partWithLowestMagnitude({workspace.Part, workspace.Part1, workspace.Part2, workspace.Part3}, compareTo)
``````

Itâ€™s the same thing in the end, your method just utilizes more library methods which do the same thing under the hood. Itâ€™s not optimizing the code other than reducing 1 or two lines.

1 Like

I am not saying it optimizes the code, I just mean `table.create` could be used for optimization in array cases, also I made a slight error in the code so Iâ€™m going to edit and fix it. `table.find` only works on arrays which I forgot.

I actually think the solution script is better because you can set a maximum distance case opposed to none.