PathFinding issue

Hello everyone,
I have been trying to make a pathfinding Npc which follows the player when it sees him
it works perfectly but The npc moves strangely.
here is the script

local pathfinding = game:GetService("PathfindingService")
local path = pathfinding:CreatePath()
local Npc = game.Workspace.Rig
game.ReplicatedStorage.SeenRemote.OnServerEvent:Connect(function(Plr)
	path:ComputeAsync(Npc.HumanoidRootPart.Position , Plr.Character.HumanoidRootPart.Position)
	local Waypoints = path:GetWaypoints()
	for i,waypoint  in pairs(Waypoints) do
		Npc.Humanoid:MoveTo(waypoint.Position)
		Npc.Humanoid.MoveToFinished:Wait()
	end
end)

and here is the client

local runservice  = game:GetService("RunService")
local Plr = game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Npc = workspace.Rig
runservice.RenderStepped:Connect(function(A)
	local PlrtoNpc = (Char.Head.Position - Npc.Head.Position).Unit
	local Npclook = Npc.Head.CFrame.LookVector
	local producthead = PlrtoNpc:Dot(Npclook)
	if producthead > .4  then
		wait(1)
		game.ReplicatedStorage.SeenRemote:FireServer()
	end
end)

here is the video gives more detail about the issue
robloxapp-20240123-1732009.wmv (2.7 MB)


1 Like

Can you tell me where the local script is located? Because sometimes it runs twice if you place it somewhere in StarterPlayer. If it is running twice try putting it in StarterGui and then put it in a ScreenGui.

Its in the starterCharacterScripts

Try putting it in StarterGui and then in a ScreenGui…

It didn’t work β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™

You could try debugging the code by using print statements.
Like this:

	for i,waypoint  in pairs(Waypoints) do
		Npc.Humanoid:MoveTo(waypoint.Position)
		Npc.Humanoid.MoveToFinished:Wait()
        print('Move finished!') 
	end

Just to check if something is acting weird or running incorrecly/too many times.

It decreased its zigzag movement but its still moving zigzag

Can you show me what it prints in the ouput?
(I just want to make sure it isn’t an issue where the code is running too many times)

somehow there is no any prints

Do you think because the HumanoidRootPart of the Npc isnt in the position of the player’s HumanoidRootPart?

Should i change the point that should be reachedβ€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™β€˜β€™

Maybe add a task.wait()? It is kinda weird it didn’t print anything in the ouput.

You can add a certain amount of time that the script has to wait like 0.1 or 0.5 incase just task.wait() doesn’t work.

Task.wait(0.1) made it laggy but decreased its zigzag movement

I do think it’s an issue where something is running too many times since just 1 task.wait() normally doesn’t cause any lag. But other than that i’m starting to run out of ideas.

i think i should change the point and make space between the player and the npc but how can i do this