Help with getting closest enemy

Hey, so I have a table of enemies, and I want to sort the table, so I have the closest enemy at the top, all the way down to the farthest enemy at the bottom., How would I do this? I am a noob at tables and dictonaries, so I would like someone to point me in the right direction.

I am a noob at tables and dictionaries…

No problem. I don’t know why, but, unlike other coding languages, sorting arrays wasn’t one of the first things I learned in Luau.

Anyway, since you want to sort a table, you can use a built-in table function: table.sort(). The first parameter is the table you want to sort, and the second parameter is a callback function that requires two arguments (a and b, which represents two elements in the array that will be compared) and will return a boolean value for every a and b that will determine if b is placed in front of a, or if the two elements’ orders will stay the same.

If the function returns true, a will be placed before b (b would be placed after a); if false gets returned, the elements won’t be repositioned.

For sorting the values from least to greatest, you can just call the table.sort() function on a table (table.sort(array)). Using the default callback function with an array of numbers is the same as the following callback function:

local numbers = {8, 1, 2, 6}

table.sort(numbers, function(a, b)
    -- If `a` was 2 and `b` was 8, this would return true.
    -- So, `b` would be placed in front of `a`.
    return a < b
end)

As I stated above, though, you can just type this:

table.sort(numbers)

You can sort from greatest to least by returning a > b instead (which would require a custom callback).

To compare the axes of Vector3s, your callback would look something like this:

-- This example also displays using properties of `a` and `b`.
table.sort(positions, function(a, b)
    return x.Y < b.Y -- Compares Y axes; sorts from least to greatest.
end)

Using this logic, you’d first calculate the magnitude between a and whatever you want to get the distance from (for example, the player) and do the same for b, then compare those magnitudes (magnitudeA > magnitudeB).
In case you don’t know how to calculate the distance between two Vector3s, you’d use this formula (if that is what it is called): (position1 - position2).Magnitude.

This is just simple sorting with number values. table.sort can be used with more than just numbers, a boolean just needs to be returned in the callback function.

Here is the documentation for table.sort() if you want to know extra details that I may have left out: table.sort() documentation.

2 Likes

Wow, that helps a lot! But how exactly would you use table.find() to get the first object in a a table, and then the next, and then the next… Would I use something like this?

for index = 1, #tableOfEnemies do

print(index)

end

Yes, that would get the indexes of the items in the table. If you want to get the item, too, you would use a for i, v loop:

for index, value in array do
    print(index, value)
end

Use table.find() when you don’t know the index of an item, or if you want to check if that item is even in the table:

local names = {"Jayde", "Leonardo", "Mazi"}
local name1, name2 = "Edward", "Jayde"

print(table.find(names, name1)) -- Output: nil.
print(table.find(names, name2)) -- Output: 1

Here’s the documentation for table.find(): table.find() Documentation.

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