My NPC attacks players and other NPCs (including clones of itself) and I was wondering if there is a way for it to chase just players. Here’s the code.
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local myHead = script.Parent:WaitForChild("Head")
local tweenService = game:GetService("TweenService")
local curTarget
function findTarget()
local target
local dist = 10000000000000000000000000000000000000000000000
for i,v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local rootPart = v:FindFirstChild("HumanoidRootPart")
if human and rootPart and v ~= script.Parent then
if (myRoot.Position - rootPart.Position).magnitude <= dist and human.Health > 0 then
dist = (myRoot.Position - rootPart.Position).magnitude
target = rootPart
end
end
end
return target
end
function checkSight(target)
local ray = Ray.new(myHead.Position,(target.Position - myHead.Position).Unit * 1000)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
if hit then
if hit:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then
if math.abs(hit.Position.Y - myRoot.Position.Y) < 2 then
return true
else
return false
end
else
return false
end
else
return false
end
end
local doorDebounce = true
myHuman.Touched:Connect(function(obj)
if obj.Name == "DoorFrame" and doorDebounce == true then
doorDebounce = false
local opened = obj.Parent:FindFirstChild("Opened")
local hinge = obj.Parent:FindFirstChild("Hinge")
local hingeOpened = obj.Parent:FindFirstChild("HingeOpened")
local doorLock = obj:FindFirstChild("DoorLock")
local doorOpen = obj:FindFirstChild("DoorOpen")
if opened and hinge and hingeOpened and doorLock and doorOpen then
if opened.Value == false then
doorLock:Play()
opened.Value = true
myHuman.WalkSpeed = 0
obj.CanCollide = false
wait(.25)
doorOpen:Play()
tweenService:Create(hinge,TweenInfo.new(.35,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{CFrame = hingeOpened.CFrame}):Play()
wait(.45)
coroutine.resume(coroutine.create(function()
wait(2)
obj.CanCollide = true
end))
myHuman.WalkSpeed = 14
end
end
end
doorDebounce = true
end)
while true do
local target = findTarget()
if target then
local sight = checkSight(target)
if sight == true then
repeat wait()
myHuman:MoveTo(target.Position)
until checkSight(target) == false
elseif sight == false then
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position,target.Position)
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(path:GetWaypoints()) do
myHuman:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
if checkSight(target) == true then
break
end
local timeOut = myHuman.MoveToFinished:Wait(.5)
if not timeOut then
break
end
end
else
local door
local dist = 1000
for i,v in pairs(workspace:GetDescendants()) do
local doorFrame = v:FindFirstChild("DoorFrame")
if doorFrame then
local doorClose = doorFrame:FindFirstChild("DoorClose")
local opened = v:FindFirstChild("Opened")
if opened and doorClose then
if (myRoot.Position - doorFrame.Position).magnitude <= dist then
dist = (myRoot.Position - doorFrame.Position).magnitude
door = doorFrame
end
end
end
end
if door then
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position,door.Position)
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(path:GetWaypoints()) do
myHuman:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
if checkSight(target) == true then
break
end
local timeOut = myHuman.MoveToFinished:Wait(.5)
if not timeOut then
break
end
end
end
end
end
end
end
wait(.25)
end```