How to detect which Huamnoid is closest to the player?

Hey, I have a script problem.
Basically, I need a way for RenderStepped to check which event Humanoid is nearest to.

All zombies (humanoids) are parented to the Zombie folder, which is located in the workspace.
i want to compare all distances and always pick the one that’s closest to the player.
I know it’s possible, I just have absolutely NO IDEA how to do it.

Any help will be very appreciated :grinning:

1 Like

If I’m understanding you question correctly, you want to identify the closest zombie.

Here’s one method to do it:

local CharacterRoot = Character.HumanoidRootPart

function FindNearestZombie()
    local Zombies = workspace.ZombieFolder:GetChildren()
    local NearestZombie = Zombies[1]

    for i = 2, #Zombies do
        local Distance1 = (NearestZombie.HumanoidRootPart.Position - CharacterRoot.Position).Magnitude
        local Distance2 = (Zombies[i].HumanoidRootPart.Position - CharacterRoot.Position).Magnitude

        if Distance2 < Distance1 then
            NearestZombie = Zombies[i]
        end
    end
end
7 Likes

Yes, you understood me correctly… I will definitely check it out. Thank you for your effort!

1 Like

Basically, you need to loop through all of the zombies, get the magnitude of the zombies to the player, and put it in a table, then you will need to sort the table and get the first entry in the table, which is the closest humanoid:

function findClosestZombie()
local tbl = {}

for _,v in pairs(zombies:GetChildren()) do
    tbl[#tbl+1] = {v; (v.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude}
end

table.sort(tbl, function(a, b)
   return a[2] < b[2]
end

end
3 Likes

Ill check it out, thank you for all the effort :grinning:

Not sure if i’m doing something wrong, i edited of your code a bit and everytime i run it the Zombies[1] value is set to nil:

function findClosestZombie()
     local Zombies = {}
     for _,v in pairs(Infected:GetChildren()) do

          Zombies[#Zombies+1] = {v; (v.Head.Position -                
          Player.Character.HumanoidRootPart.Position).Magnitude}
          end
table.sort(Zombies, function(a, b)
return a[2] < b[2]
end)
Closest = Zombies[1]
print(Zombies[1])
end

You can raycast to the humanoidRootPart of every player in the game & find the magnitude. The lowest magnitude is 0 which means that the lowest magnitude value would be the closest while the highest would be the farthest.

Hope this helps!

Why would you use a raycast when you can just check the magnitude of the distance vector between a zombie’s root and a character root? You’re unnecessarily incurring expense by shooting out a ray which could potentially be very long. That is compared to vector subtraction and checking the magnitude of the resultant vector.

1 Like

Raycasts allow for you to specify range which is powerful and can be used in the scenario quite nicely. Whereas looping a set number of times where the player could be at any range is quite expensive in itself.

What about obstructions between players and zombies at the time of raycasting? You’d have to raycast twice for each obstruction, and there can be multiple objects between a player and a zombie.

1 Like

Raycasts have the option to include an ignore list.

It’s not expensive? You underestimate the power of the engine to perform a loop over an array, subtract two vectors and access the magnitude property of the resulting vector.

Long raycasts are expensive to perform and it’s completely unnecessary work when you can simply check the magnitude of the resultant vector after subtracting both root part positions. In addition, you cannot specify raycast length, it’s a math operation of extending the unit vector by a certain length.

Raycasting is completely unnecessary and pointlessly incurs expense, especially in this kind of a situation. I do not recommend doing it at all. If you want a proper solution to check the distance between two objects, use vector subtraction.

3 Likes

The performance drop comes more from the magnitude or range of your ray casts rather than the quantity. While it can be expensive, it shouldn’t drop performance considerably as you’re trying to paint.

What’s considered a long raycast? The magnitude of a ray can be altered to fit almost any specification. I can’t imagine OP needing an insane amount. If I had to guess, 30 or 50 would do just fine.

If you’re looping players specifically to see if they’re closer to the player, that’s expensive on the server. Instead, if you raycast on the client and find which player is near (not only is it more accurate), but less work. The character’s range from the other player would be the magnitude of that raycast.