How do you get a enemy NPC to lock onto (and follow) a player?
This is the revised version of my previous question. -- The main problem with the code below is that the battledroid (the enemy npc in my game) will not lock onto the player, but will lock onto a "drooling zombie" (test npc). There's a line of code that checks if a humanoid is there and if it isn't it'll check for a zombie. What can I do to make it so the droid (enemy npc) will lock onto the player? I'll give the code as well as the link to the game below.
(https://www.roblox.com/games/4931576856/Star-Wars-The-Citadel-Challenge)
01 | local larm = script.Parent:FindFirstChild( "Left Arm" ) |
02 | local rarm = script.Parent:FindFirstChild( "Right Arm" ) |
04 | function findNearestTorso(pos) |
05 | local list = game.Workspace:children() |
13 | if (temp 2. className = = "Model" ) and (temp 2 ~ = script.Parent) then |
14 | temp = temp 2 :findFirstChild( "Right Arm" ) |
15 | human = temp 2 :findFirstChild( "Humanoid" ) |
16 | if (temp ~ = nil ) and (human ~ = nil ) and (human.Health > 0 ) then |
17 | if (temp.Position - pos).magnitude < dist then |
19 | dist = (temp.Position - pos).magnitude |
27 | human = script.Parent:findFirstChild( "Humanoid" ) |
28 | if human = = nil then human = script.Parent:findFirstChild( "Zombie" ) end |
32 | local target = findNearestTorso(script.Parent.Torso.Position) |
34 | human:MoveTo(target.Position, target) |
35 | human.TargetPoint = target.Position |