NPC Following Script Closest Distance

I got a script that used for npc that should follow you, it all works but the problem is that when he reaches closest distance from you, he just stops and doing nothing. How do I make that npc will always look at you when he stop?

I tried to use CFrame.LookAt, but its working weirdly, when you stand on npc or just jump around npc , his whole body just turns at yours position

There is script:

local NPC = workspace.Dummy
function getClosestPlayer()
    local closest_player, closest_distance = nil, 200
    for i, player in pairs(workspace:GetChildren()) do
        if player:FindFirstChild("Humanoid") and player ~= NPC then
            local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
            if distance < closest_distance then
                closest_player = player
                closest_distance = distance
            end
        end
    end
    return closest_player, closest_distance
end
while true do
    local player, distance = getClosestPlayer()
    if player and distance > 10 then
        NPC.Humanoid:MoveTo(player.PrimaryPart.Position)
        local stopped 
        NPC.Humanoid.MoveToFinished:Connect(function()
            stopped = true
        end)
        repeat 
            local distance = (NPC.PrimaryPart.Position-player.PrimaryPart.Position).Magnitude
            if distance < 10 then
                NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
                break
            end
            wait()
        until stopped == true
    else
        wait(1)
    end
    
end
1 Like

Hello There!

If I’m understanding it right, the NPC try to look at you on top of him and falls.

This can be solved with the CFrame.LookAt by looking at CFrame.new(player.PrimaryPart.Position.X, NPC.PrimaryPart.Position.Y, player.PrimaryPart.Position.Z)

Hope this helps :wink:

2 Likes


ehh, cant test it, it somehow bugs

1 Like

sorry, but its not working, npc just keep teleporting to me.

1 Like

i just want to make that when his distance from you and himself is lower than in script, he just looking you until you dont get away from him

Try this

local NPC = workspace.Dummy
function getClosestPlayer()
    local closest_player, closest_distance = nil, 200
    for i, player in pairs(workspace:GetChildren()) do
        if player:FindFirstChild("Humanoid") and player ~= NPC then
            local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
            if distance < closest_distance then
                closest_player = player
                closest_distance = distance
            end
        end
    end
    return closest_player, closest_distance
end
while true do
    local player, distance = getClosestPlayer()
    if player and distance > 10 then
        NPC.Humanoid:MoveTo(player.PrimaryPart.Position)
        local stopped 
        NPC.Humanoid.MoveToFinished:Connect(function()
            stopped = true
        end)
        repeat 
            local distance = (NPC.PrimaryPart.Position-player.PrimaryPart.Position).Magnitude
            if distance < 10 then
                NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
                break
            end
            wait()
        until stopped == true
    else

NPC.PrimaryPart.CFrame.LookAt(CFrame.new(player.PrimaryPart.Position.X, NPC.PrimaryPart.Position.Y, player.PrimaryPart.Position.Z)) -- The Thing I Added

        wait(0.1) -- Changed from 1 so the looking animations is not laggy
    end
    
end

Don’t know exaclty how to write the CFrame.LookAt so maybe it doesn’t work

It’s still not working, i tried to remake your script, but came up with this:

local NPC = script.Parent
function getClosestPlayer()
	local closest_player, closest_distance = nil, 1000
	for i, player in pairs(workspace:GetChildren()) do
		if player:FindFirstChild("Humanoid") and player ~= NPC then
			local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
			if distance < closest_distance then
				closest_player = player
				closest_distance = distance
			end
		end
	end
	return closest_player, closest_distance
end
while true do
	local player, distance = getClosestPlayer()
	if player and distance > 10 then
		NPC.Humanoid:MoveTo(player.PrimaryPart.Position)
		task.wait(0.1)
	else
		NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
		NPC.PrimaryPart.CFrame = CFrame.new(NPC.PrimaryPart.Position,player.PrimaryPart.Position)
		task.wait(0.1) -- Changed from 1 so the looking animations is not laggy
	end

end

thats what’s happening when he is looking at you.


as you can see, instead of just looking in your direction, he rotates whole body at your position.

1 Like

Ok, maybe now it’ll work

local NPC = script.Parent
function getClosestPlayer()
	local closest_player, closest_distance = nil, 1000
	for i, player in pairs(workspace:GetChildren()) do
		if player:FindFirstChild("Humanoid") and player ~= NPC then
			local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
			if distance < closest_distance then
				closest_player = player
				closest_distance = distance
			end
		end
	end
	return closest_player, closest_distance
end
while true do
	local player, distance = getClosestPlayer()
	if player and distance > 10 then
		NPC.Humanoid:MoveTo(player.PrimaryPart.Position)
		task.wait(0.1)
	else
		NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
		NPC.PrimaryPart.CFrame = CFrame.new(NPC.PrimaryPart.Position, Vector3.new(player.PrimaryPart.Position.X, NPC.PrimaryPart.Position.Y, player.PrimaryPart.Position.Z)) --Changed here from player.PrimaryPart.Position
		task.wait(0.1) -- Changed from 1 so the looking animations is not laggy
	end

end

The problem is that it tries to follow the Y axis, to adjust this you just need to make it follow the X and Z axis of the player and it’s own Y axis coordinates.

Thank you for helping me, its working.

1 Like