How do i make sure the NPC hunts down the closest Humanoid?
My current script lets the NPC hunt the player who joined last, the NPC doesnt change target until that person leaves. I need to script it so that the NPC hunts the player who is closest, i tried to work it out myself with IntValues. Please someone help me!
01 | function getHumanoid(model) |
02 | for _, v in pairs (model:GetChildren()) do |
03 | if v:IsA 'Humanoid' then |
10 | local ai = script.Parent |
11 | local human = getHumanoid(ai) |
12 | local hroot = ai.HumanoidRootPart |
13 | local zspeed = hroot.Velocity.magnitude |
16 | local pfs = game:GetService( "PathfindingService" ) |
18 | function GetPlayerNames() |
19 | local players = game:GetService( 'Players' ):GetChildren() |
21 | for _, v in pairs (players) do |
23 | name = tostring (v.Name) |
35 | function GetPlayersBodyParts(t) |
38 | local figure = torso.Parent |
39 | for _, v in pairs (figure:GetChildren()) do |
45 | return "HumanoidRootPart" |
49 | function GetTorso(part) |
50 | local chars = game.Workspace:GetChildren() |
52 | for _, v in pairs (chars) do |
53 | if v:IsA 'Model' and v ~ = script.Parent and v.Name = = GetPlayerNames() then |
54 | local charRoot = v:FindFirstChild 'HumanoidRootPart' |
55 | if (charRoot.Position - part).magnitude < SearchDistance then |