Pathfinding script not working properly (Solved)

I have a path finding script in a npc that I set to go towards a part. It will go to the part, and when I move to the part it will move to the new position. BUT If I set it to the player’s humanoid root part, it goes to where the player spawned in but not where the player is moving.

Edit: The npc now does change direction torwards the player but completes the original path first before starting a new one

New edited script:

local PathfindingService = game:GetService("PathfindingService")

local Humanoid = script.Parent.Humanoid
local Root = script.Parent:FindFirstChild("HumanoidRootPart")
--local goal = game.Workspace.Test 
local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()
local function UpdatePath()
	path:ComputeAsync(Root.Position, goal.Position)
	local waypoints = path:GetWaypoints()

	task.wait() 
	for i, waypoint in ipairs(waypoints) do
		path:ComputeAsync(Root.Position, goal.Position)
		local waypoints = path:GetWaypoints()

		Humanoid:MoveTo(waypoint.Position)

		if waypoint.Action == Enum.PathWaypointAction.Jump then
			Humanoid.Jump = true
		end
		Humanoid.MoveToFinished:Wait()
	end
end

spawn(function()
	while true do
		wait()
		UpdatePath() 
	end
end)

goal:GetPropertyChangedSignal("Position"):Connect(UpdatePath)

So ye help would be cool

2 Likes

Maybe, because it’s not updating?
Try making a While wait() do function
So like this

While wait() do
     UpdatePath()
end
1 Like

Well it is supposed to update. Like I said how it updates with the part, but not the player.

Can you add a While wait() do?

This is a temporary solution, wait() is not as efficient and decreases performance. I would recommend game:GetService("RunService").Stepped:Wait() for a more permanent solution.

But, both should work. RunService should give better performance, though.

1 Like

That seemed to do the trick, but the npc still completes its original path before creating a new one.

I don’t think he is using a Local script.

Ya I’m using a server script…


RenderStepped requires a LocalScript, Stepped and Heartbeat work on both.

Did you learn something today?

Yup, Never knew that before.
Everyday I learn something new.

1 Like

@lilmazen1234 but uh guys to what I was saying about that the path finishes first before starting a new one…

Yes, right. Isn’t that logic?

Ok, so if you really want to do that you can add a spawn, which will start the function in a separate thread. You just need to know where to place it.

Add a spawn? Could you elaborate?


spawn(function()
while true do
--code here
end
end)

All of the code?


Yes, you just replace that specific section, but keep the while loop.

Didnt work. This is what I did:




spawn(function()
	while true do
		wait()
		--code here

		local PathfindingService = game:GetService("PathfindingService")

		local Humanoid = script.Parent.Humanoid
		local Root = script.Parent:FindFirstChild("HumanoidRootPart")
		--local goal = game.Workspace.Test 
		local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
		local path = PathfindingService:CreatePath()
		local function UpdatePath()
			path:ComputeAsync(Root.Position, goal.Position)
			local waypoints = path:GetWaypoints()

			task.wait() 
			for i, waypoint in ipairs(waypoints) do
				path:ComputeAsync(Root.Position, goal.Position)
				local waypoints = path:GetWaypoints()

				Humanoid:MoveTo(waypoint.Position)

				if waypoint.Action == Enum.PathWaypointAction.Jump then
					Humanoid.Jump = true
				end
				Humanoid.MoveToFinished:Wait()
			end
		end
		while wait() do
			UpdatePath() 

		end

		goal:GetPropertyChangedSignal("Position"):Connect(UpdatePath) 

	end
end)

Not exactly. Instead, I want you to place it around the smaller loop.

1 Like

Same results, this is what I did:



		local PathfindingService = game:GetService("PathfindingService")

		local Humanoid = script.Parent.Humanoid
		local Root = script.Parent:FindFirstChild("HumanoidRootPart")
		--local goal = game.Workspace.Test 
		local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
		local path = PathfindingService:CreatePath()
		local function UpdatePath()
			path:ComputeAsync(Root.Position, goal.Position)
			local waypoints = path:GetWaypoints()

			task.wait() 
			for i, waypoint in ipairs(waypoints) do
				path:ComputeAsync(Root.Position, goal.Position)
				local waypoints = path:GetWaypoints()

				Humanoid:MoveTo(waypoint.Position)

				if waypoint.Action == Enum.PathWaypointAction.Jump then
					Humanoid.Jump = true
				end
				Humanoid.MoveToFinished:Wait()
			end
		end
	
spawn(function()
	while true do
		wait()
		UpdatePath() 
		end
	end)

goal:GetPropertyChangedSignal("Position"):Connect(UpdatePath)

Wait this is the wrong position. Let me just send you the finished product.

local PathfindingService = game:GetService("PathfindingService")

		local Humanoid = script.Parent.Humanoid
		local Root = script.Parent:FindFirstChild("HumanoidRootPart")
		--local goal = game.Workspace.Test 
		local goal = game.Workspace:WaitForChild("trueblockhead101"):WaitForChild("HumanoidRootPart")
		local path = PathfindingService:CreatePath()
		local function UpdatePath()
			path:ComputeAsync(Root.Position, goal.Position)
			local waypoints = path:GetWaypoints()
			for i, waypoint in ipairs(waypoints) do
				path:ComputeAsync(Root.Position, goal.Position)
				local waypoints = path:GetWaypoints()

				Humanoid:MoveTo(waypoint.Position)

				if waypoint.Action == Enum.PathWaypointAction.Jump then
					Humanoid.Jump = true
				end
				wait(3)--Change this.
			end
		end
	
UpdatePath()