How do you get an enemy NPC to follow the player?
Asked by
3 years ago Edited 3 years ago
Disclaimer (I'm very new to coding within Roblox and 90% of my game code is from the toolbox, and the battledroid I'm using is also from there; hence my being confused on how to fix it)
The Battledroid NPC won't attack me, but will attack a "drooling zombie". All I need for the Battle Droid to do is to follow me because it already does damage upon touching the player, and it will already shoot and damage the player. - - Could other scripts within my game be affecting it's response? I already deleted the spawn protection wall because I thought it might be messing with it. But it's probably the code which I'll add underneath. Thanks for any help!!
(Btw this is the link to the game: 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 |