I have a tower defense game, and I cant figure out how to find the lowest value from a table that contains instances and how far along they are along the path:
{
[Instance(ID)] = distance to treasure
}
Ive tried multiple methods to find the lowest value and get the instance from it, but i couldnt get it working. Heres the script:
function PlayLandUnit(tower, map)
local rangePart = tower.Range
local foundMobs = {}
local target
while task.wait() do
task.spawn(function()
local foundTouching = workspace:GetPartsInPart(rangePart, OverlapParams.new())
for i, v in pairs(foundTouching) do
if v:IsDescendantOf(map.mobs) then
foundMobs[v.Parent] = v.Parent.Distance.Value
end
end
target = -- get the index with the lowest value from foundMobs somehow here
if target then
print(target)
end
end)
end
end
I’m guessing this is the table with the values you need.
local t = {}
local target
for i, v in pairs(foundMobs) do
table.insert(v)
end
local lovestVal = math.min(table.unpack(t))
for i, v in pairs(foundMobs) do
if v == lowestVal then
target= v
end
end