Enemy npc not tracking player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the enemy npc to track down only players but not other npcs
  2. What is the issue? Include screenshots / videos if possible!
    My script does not work the enemy npc only stands still and not move at all
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have found a post really similar to my script but when i tried transferring the script over to my script it didnt work
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Here is my script:

local NPC = script.Parent
local debounce = false

NPC.NPCHumanoid.Touched:Connect(function(hit)
if hit and game.Players:GetPlayerFromCharacter(hit.Parent) and NPC.NPCHumanoid.Health > 1 then
if debounce == false then
debounce = true
hit.Parent.Humanoid:TakeDamage(25)
wait(.25)
debounce = false
end
end
end)

function FindPlayer(Position)
local playerList = game:GetService(“Players”):GetChildren()
local Torso = nil
local Distance = 50
local HumanoidRootPart = nil
local Humanoid = nil
local Player = nil

for i, plr in pairs(playerList) do
	local playerCharacter = plr.Character or plr.CharacterAdded:Wait()
	
	if playerCharacter then
		HumanoidRootPart = playerCharacter:FindFirstChild("HumanoidRootPart")
		Humanoid = playerCharacter:FindFirstChild("Humanoid")
		
		if (HumanoidRootPart ~= nil) and (Humanoid ~= nil) and (Humanoid.Health > 0) then
			if (HumanoidRootPart.Position - Position).Magnitude < Distance then
				Torso = HumanoidRootPart
				Distance = (HumanoidRootPart.Position - Position).Magnitude
			end
		end
	end
end

end

while true do
wait(1)
local Target = FindPlayer(script.Parent.HumanoidRootPart.Position)
if Target ~= nil then
script.Parent.NPCHumanoid:MoveTo(Target.Position, Target)
end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Forgive me if I am unclear with responses given as I am still a new developer and some of the code that I found was from the internet so I may not understand it myself

A very simple method I employ, which is both efficient in performance and adept at tracking the nearest player, is as follows:

  1. By employing a sizable hitbox around the NPC with CanCollide disabled, I can easily detect whether they’ve entered or made contact with that hitbox.

You can utilize Zone Plus for this purpose. Here’s the link: ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries

Well you haven’t returned anything? The Torso Variable has been set, but hasn’t got returned, so there is no way you can get the target since you only checking but not sending. After the for loop you need to return it, basically after its end.

Thanks for the solution! The script works now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.