Enemy NPC keeps on going back and forth

Ok, so i tried making an ncp that tracks the player, but it keeps on going back and forth, which slows him down alot.

My code:
local hum = script.Parent
local HumRootPart = hum.Parent.PrimaryPart
local RServ = game:GetService("RunService")
local plrs = game:GetService("Players")
local TargetDist = 100
local StopDist = 5
local anim = script.Parent:LoadAnimation(script.Parent.Parent.Run)
local PathFindingService = game:GetService("PathfindingService")
local Kiddo = workspace:FindFirstChild("WanderFolder"):GetChildren()
local ChildInBasement = math.random(1,#Kiddo)
local RandomInt = Kiddo[ChildInBasement]
local Waypoints = workspace.WanderFolder:GetChildren()
local AttaccDeeztance = 7
local Event = game:GetService("ReplicatedStorage").RemoteEvents.Jumpscares.Experiment001.SpookiExpewimente
local AttaccWait = 1
local LastAttacc = tick()
local path = PathFindingService:CreatePath()  

function FindNearestPlayer()
	local plrList = plrs:GetPlayers()
	local nearestPlr = nil
	local dist = nil
	local direction = nil
	for _, player in pairs(plrList) do
		local char = player.Character
		if char then
			local distVector = (player.Character.HumanoidRootPart.Position - HumRootPart.Position)
				if not nearestPlr then
					nearestPlr = player
					dist = distVector.Magnitude
					direction = distVector.Unit
				elseif distVector.Magnitude < dist then
					nearestPlr = player
					direction = distVector.Unit
					dist = distVector.Magnitude

	return nearestPlr, dist, direction

	local nearestPlr, distance, direction = FindNearestPlayer()
	if nearestPlr then
		if distance <= TargetDist and distance >= StopDist then
			path:ComputeAsync(hum.Parent.PrimaryPart.Position, nearestPlr.Character.PrimaryPart.Position)
			local Waypoints = path:GetWaypoints()
			for i, Waypoint in pairs(Waypoints) do
				if Waypoint.Action == Enum.PathWaypointAction.Jump then
					hum.Jump = true

Any help is appeciated!

also, i will probably remove this part because it might glitch him to get stuck in places…

You could probably find an answer somewhere in this forum already but
You can try changing bots network owner to nil

Ok, i’ll try it and i’ll tell you if it worked

1 Like

Once the studio finally decides to open the place i have to edit

Also, i am pretty bad at coding, how do we change the network owner to nil

You could do it this way for example

for _,v in pairs(character:GetChildren()) do
	if v:IsA('Part') then

Thank you. I will tell you if it worked!

Edit: i cant do it rn because my roblox studio is acting funny and not opening

1 Like

It pretty much worked, thank you so much

no problem i am happy that i ve helped you

I’ve got a new error, basically, it chases you and then it might randomly stays facing a wall without moving at all…
Maybe its just me who accidentaly dragged him to a position where he was supposed to just to catch me and the fact that he couldnt jump broke him

(i’m making a horror game)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.