I need help with my NCP script

1.this my issue… on the video I record it with a Roblox recorder.

robloxapp-20210426-1100176.wmv (3.6 MB)

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

I will try this and let you know if it works.

Better to “get” list from game.Players: GetChildren () bc you don’t need to run for loop trough every obejct in workspace

need help please on the ncp for my game Im making

Try this:

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

it didn’t work the NCP was just standing there

In your original script, there is no if statement that checks if the target is a Player.

You can check if something is a player by finding it’s model, and doing the following code

if game.Players:GetPlayerFromCharacter(modelGoesHere) then
     -- More Code Here
end

it didn’t work is it okay if I give you access to edit in-game to look into the script for me because I really need help.

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
1 Like

Make sure it is not anchored or with a PlatformStand.

1 Like

Thank you, so much it worked!!