Pathfinding script not updating

So basically I want to create a new path each milisecond, because the npc moves to the player constantly.

Well, the output says nothing, but it moves to the previous player position, then when finished it moves to the new position.

I tried modifying the script, for example:
I removed the hum.MoveToFinished:Wait(), but then it wasn’t pathfinding.

I don’t really remember what else I did.

npc.Events.FaceViewed.OnServerEvent:Connect(function(plr)
	if not triggered and target == nil and canBeTriggered then
		canBeTriggered = false
		sounds.Idle:Stop()
		triggered = true
		sounds.Panick:Play()
		wait(18.128)
		sounds.Rage:Play()
		repeat
			wait()
			local ps = game:GetService("PathfindingService")

			local hum = npc.Humanoid
			local dest = plr.Character.HumanoidRootPart.Position

			local path = ps:CreatePath()

			path:ComputeAsync(npc.HumanoidRootPart.Position, dest)

			local waypoints = path:GetWaypoints()

			for _, waypoint in pairs(waypoints) do
				hum:MoveTo(waypoint.Position)
				hum.MoveToFinished:Wait()
				waypoint = nil
			end
		until plr.Character.Humanoid.Health == 0
	end
end)

I hope you can help me, thanks if you do.

1 Like

The problem here is you are looping the entire waypoints generated array each generation. You will want to only move to the first waypoint and then generate a new path.
Remove the for loop and just use

hum:MoveTo(waypoints[1].Position)
hum.MoveToFinished:Wait()
1 Like

Well, heres the updated code. For some reason its not working.

npc.Events.FaceViewed.OnServerEvent:Connect(function(plr)
	if not triggered and target == nil and canBeTriggered then
		canBeTriggered = false
		sounds.Idle:Stop()
		triggered = true
		sounds.Panick:Play()
		wait(18.128)
		sounds.Rage:Play()
		repeat
			wait()
			local ps = game:GetService("PathfindingService")

			local hum = npc.Humanoid
			local dest = plr.Character.HumanoidRootPart.Position

			local path = ps:CreatePath()

			path:ComputeAsync(npc.HumanoidRootPart.Position, dest)

			local waypoints = path:GetWaypoints()

			hum:MoveTo(waypoints[1].Position)
			hum.MoveToFinished:Wait()
		until plr.Character.Humanoid.Health == 0
	end
end)

Sorry about that! waypoints[1] will most likely be too close to the NPC and will be considered “MovedTo”. You can use waypoints[2] instead but Pathfinding can be tricky and sometimes even waypoints[2] will be too close. I will see if I can manage a better solution than that.

Makes sense. I hope you can find a solution, I’m not so advanced with pathfinding.

I would actually say that the final resulting position will be “close enough” that you wouldn’t notice in 99% of cases (We are talking about < 1 stud differences). So, just use this and you should be fine. Also, remove ps from the loop.

local ps = game:GetService("PathfindingService")

repeat
	wait()

	local hum = npc.Humanoid
	local dest = plr.Character.HumanoidRootPart.Position

	local path = ps:CreatePath()

	path:ComputeAsync(npc.HumanoidRootPart.Position, dest)

	local waypoints = path:GetWaypoints()

	hum:MoveTo(waypoints[2].Position)
until plr.Character.Humanoid.Health == 0

[edit] Formatting

the for loop inside the repeat until loop means that, the NPC will try complete the path set and then find the new path to fix this i would remove the “for _, waypoints in…” loop and just have hum:MoveTo(waypoint[1].Position
and then the rest

It works great, but it keeps stopping for about 1/10 of a second. I’m fine with that unless theres a way to prevent that.

If you are getting this behavior outside of studio testing, I’d assume it’s because of network ownership of the NPC switching back and forth. Set the networkownership of the NPC to nil and that may help out.

It could also be the wait() yielding for a longer time in which you could tie into .Heartbeat:Wait(). Give the networkownership a shot first.

Big thanks.

The 1st solution (:SetNetworkOwner(nil)) didn’t really change anything.

The 2nd solution(remove the wait()) worked and I thank you again.

1 Like

Great to hear! Have fun and hope all goes smoothly from here on out.

Thanks. I’ll tell you if theres any new errors but I hope theres none.

Sadly, there are 2 new problems.

First, the first npc doesn’t use the walkspeed, heres the script:

canBeTriggered = false
		sounds.Idle:Stop()
		triggered = true
		sounds.Panick:Play()
		wait(18.128)
		sounds.Rage:Play()
		local ps = game:GetService("PathfindingService")
		
		local pathParams = {
			AgentRadius = 2,
			AgentHeight = 7,
			AgentCanJump = false
		}
		
		repeat
			local hum = npc.Humanoid
			local dest = plr.Character.HumanoidRootPart.Position

			local path = ps:CreatePath(pathParams)

			path:ComputeAsync(npc.HumanoidRootPart.Position, dest)

			local waypoints = path:GetWaypoints()

			hum:MoveTo(waypoints[2].Position)
		until plr.Character.Humanoid.Health == 0

Before you ask, it isn’t caused by the “AgentParameters”, because I tried.

Second problem is, I recreated the script for an other npc, this time the output isn’t blank.

The output says:

20:17:04.376 Workspace.SCPs.SCP-999.Script:42: attempt to index nil with ‘Position’ - Server - Script:42
20:17:04.376 Stack Begin - Studio
20:17:04.376 Script ‘Workspace.SCPs.SCP-999.Script’, Line 42 - Studio - Script:42
20:17:04.376 Stack End - Studio

And the script is:

local ps = game:GetService("PathfindingService")

local pathParams = {
	AgentRadius = 2,
	AgentHeight = 7,
	AgentCanJump = false
}

while wait() do
	local hum = npc.Humanoid
	local dest = closestHRP.Position

	local path = ps:CreatePath(pathParams)

	path:ComputeAsync(npc.HumanoidRootPart.Position, dest)

	local waypoints = path:GetWaypoints()

	hum:MoveTo(waypoints[2].Position)
end

By “does not work with walkspeed”, how are you setting walkspeed? Are you modifying it on the server? In studio properties? I’m fairly certain MoveTo utilizes walkspeed internally.
Also, given the server usually runs at 20hz, the higher your move speed is the more likely this method is going to cause stuttering. The interpolation from server->client will not be as smooth the faster it moves.

As for the second issue, which line is 42? There are multiple places with .Position so I am unsure which section of code it is trying to index .Position with.

I’m setting the walkspeed in properties.

Line 42 is:

hum:MoveTo(waypoints[2].Position)

This problem also occurs for the first npc. Hope you can help.

Just make sure you change the walkspeed on the Server View and not the Client view. The way you have it currently it is Server-sided so the client changing the walkspeed won’t replicate the change in movement speed.

I assume this is because the path is empty. Use the following to make sure that the computed path exists.

if path.Success == Enum.PathStatus.Success then

-- All your MoveTo code here

end

This will make sure a path is successful before trying to move through waypoints.

First, I actually meant that it keeps stopping, more frequently than before.

Second, It works. Thanks.

This should adjust for walkspeed and calculating the correct waypoint to traverse based on two factors
Walkspeed = studs per second
Waypoints are roughly 4 studs apart each waypoints

	local waypoint = math.ceil(npc.WalkSpeed/4)	
	if waypoint > #waypoints then
		waypoint = #waypoints
	end

	npc:MoveTo(waypoints[waypoint].Position)

I don’t foresee any issues with this but please let me know if you encounter any.

The problem previously was that as WalkSpeed increased, the NPC was overshooting the 2nd waypoint during the frame. This should adjust based on how fast he is traveling and choose an appropriate waypoint to match the distance covered.

Nevermind. With a little changes it works PERFECT. I don’t see any errors or issues.

First of all, in your script, you wrote:

npc:MoveTo(waypoints[waypoint].Position)

That moved the entire model (by PrimaryPart) to the waypoints.

I changed it to:

hum:MoveTo(waypoints[waypoint].Position)

And it now moves the humanoid.

Next, I had to modify the:

local waypoint = math.ceil(hum.WalkSpeed/4)

To my npc’s WalkSpeed. Now it works perfect.

Thanks for all your help.

Ahh yes the npc:MoveTo was entirely my fault. Terribly sorry about that. Glad that everything is sorted and working like a charm now. If you have any more situations arise, feel free to post or message me.