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)
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 500
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Right Arm")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
human = script.Parent:findFirstChild("Humanoid")
if human == nil then human = script.Parent:findFirstChild("Zombie") end
while true do
wait(1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
human:MoveTo(target.Position, target)
human.TargetPoint = target.Position
end
end

1 comment
On line 27 it should lock onto a humanoid but it wont lock onto a player; why is that?