I've been messing around with this script for a bit, until I finally realized I can't use table.sort() on a table of objects (the characters). So after that revelation, I realized I needed a way to return the character and also sort a table to get the smallest distance with the first index.
Any ideas? I'm stumped.
local function GetClosestCharacter()
local distTable = {}
for i, plr in pairs (Players:GetPlayers()) do
if plr ~= Player then
if Functions.IsTeamMate(plr, Player) then
if plr.Character and Character then
local distance = (plr.Character.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude
if distance <= Configuration.HealMaxDistance.Value then
table.insert(distTable, i, plr.Character)
end
end
end
end
end
table.sort(distTable)
return distTable[1]
end
Heres what I had so far, which obviously trying to compare objects is syntactically incorrect, I just cant think of a way to return the character with the shortest distance to the player.

1 comment
lol i promise that indentation isnt my fault; rip text editor on SH, needs a larger window lul