Npc Follow Player Script Not Workng

Hi, I’m trying to make a npc that follows the player while they’re in a zone (part), but the script isn’t working. There’s no errors in the output, and the script is located in the npc. Pls pls help Here’s the code:

local zonePart = game.Workspace.Zone1
local npc = script.Parent  
local humanoid = npc:FindFirstChildOfClass("Humanoid")  
local followSpeed = 16  

local function isPlayerInZone(player)
   local character = player.Character
   if character then
       local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
       if humanoidRootPart then
           return zonePart:IsPointInRegion3(humanoidRootPart.Position)
       end
   end
   return false
end

local function followPlayer(player)
   local character = player.Character
   local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
   if humanoidRootPart then
       local direction = (humanoidRootPart.Position - npc.HumanoidRootPart.Position).unit
       humanoid:MoveTo(npc.HumanoidRootPart.Position + direction * followSpeed)
   end
end

game:GetService("RunService").Heartbeat:Connect(function()
   local player = game.Players.LocalPlayer 
   if player and isPlayerInZone(player) then
       followPlayer(player)
   end
end)

zonePart.Touched:Connect(function(hit)
   local character = hit.Parent
   local player = game.Players:GetPlayerFromCharacter(character)
   
   if player then
       if isPlayerInZone(player) then
           followPlayer(player)
       else
           humanoid:MoveTo(npc.Position)  
       end
   end
end)

looking at this, this tells me your script is located under the NPC character model. However,

IF the script is indeed under the npc model in workspace, this line of code will not work, this line of code only returns the local player if nested into a localscript within an area under client control (replicatedfirst, playergui, etc). Since this is (assumingly) a server script placed inside the workspace NPC model, this line of code will not return anything. therefor, your trying to check if a non existant player value is in a region, hence why nothing happens. Using this line of code in a server script wil just return nothing, no errors or exceptions which also explains why you see no error.

Instead, use a system which constantly checks if there is any players in the workspace region (preferably add a check delay to prevent lag), then implement a logic which detects the nearest player to the npc thats also in the zone, and have the npc follow that player.

i dont have the motivation nor mental capacity at the moment to write out an entire fixed script, if you dont know how to do this, perhaps ask chatgpt to write you a code logic that detects the nearest player in the workspace. or look for posts on this forum which involve a nearest player calculation code.

if you have any more questions on how to do so, feel free to reply to my message and ill gladly help!

have a good night.

Just copy and paste it into a regular script with a context of Local


This is a regular server Script, but Players.LocalPlayer doesnt work server-side

This is a LocalScript, what you need/want, but wont work in an npc

This is a server-script with a RunContext property of Client, and it works absolutely fine anywhere except ReplicatedFirst.

TLDR? Literally what I said first.