So I was wondering how I would compare the distance between 2 or more parts to another part, and return the part with the lowest distance as I am working on an E to interact system.
I’ve tried to do this, but I wasn’t able to return the actual part, only the lowest number (or in this case distance), if that makes any sense.
When I did get it to return a part, it didn’t actually return the closest part but rather the last part in the table I sent it.
If it helps at all, here is the code I attempted to use:
--FYI, c_part stands for comparison part
function getLowestMagnitude(tbl, c_part)
local magnitudes = {}
local lowestNumber
local lastNumber
local lowestPart
for i, v in pairs(tbl) do
local magnitude = (v.Position - c_part.Position).magnitude
table.insert(magnitudes, magnitude)
end
for i, v in pairs(magnitudes) do
lowestNumber = v
lowestPart = tbl[i]
if lastNumber then
if lowestNumber < lastNumber then
lastNumber = lowestNumber
else
lowestNumber = lastNumber
end
else
lastNumber = lowestNumber
end
end
return lowestPart
end
The issue that I am having here is that it returns the last part in the table, not the part with the lowest distance.
Help would be greatly appreciated!