When going near the NCP, it comes to me as it should and I have two of the NCP’s who do the same thing their command Is to follow me. When they see each other they get together and don’t follow me anymore, I need help on them following me only the player. but also their enemy NCP instead of NCP.
Also sorry for the mislead… here the script I used
function findNearestPlayer(Position)
wait(0.3)
local List = game.Workspace:children()
local Torso = nil
local Distance = 900
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(“HumanoidRootPart”)
Human = Temp2:findFirstChild(“Humanoid”)
if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
if (Temp.Position - Position).magnitude < Distance then
Torso = Temp
Distance = (Temp.Position - Position).magnitude
end
end
end
end
return Torso
end
while true do
local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
If this is the zombie “free model” script I suggest changing the Humanoid’s name of the NPCs to anything but “Humanoid”.
Else try checking if the character is a player’s character by doing the following:
local PlayerService = game:GetService("Players")
local TargetCharacter -- This would be any target
local TargetPlayer = PlayerService:GetPlayerFromCharacter(TargetCharacter)
-- Check if the TargetPlayer is valid or nil
if TargetPlayer then
-- This is your target
end
function findNearestPlayer(Position)
wait(0.3)
local Torso,Distance = nil,900
for i,Player in pairs(game:GetService("Players"):GetPlayers()) do
local Char = Player.Character
if Char:WaitForChild("Humanoid").Health <= 0 then continue end
if Player:DistanceFromCharacter(Position) < Distance then
Torso = Char:FindFirstChild("HumanoidRootPart")
end
end
return Torso
end
while true do
local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
if target then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
I’d love to help, but I have a lot I’m working on myself.
Try this:
function findNearestPlayer(Position)
local distance = 900
local target = nil
for _, player in ipairs(game.Players:GetPlayers()) do
if player:DistanceFromCharacter(Position) < distance then
if player.Character then
target = player.Character.PrimaryPart
end
end
end
return target
end
while true do
wait(0.3)
local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end