Pathfinding works for the first time, then barely works

I want the npc to chase the player after it picks up the item and for the first time it works great, but when trying it again there’s a lot of weird things going on, the npc moves slower (the WalkSpeed should be 40), and it never kills the players etc etc. here’s a video demonstrating it

Here’s the code for the pathfinding

function chasePlayer(player, monster, monsterSettings)
	if not player.Character then return end
	if player.Character.Humanoid.Health <= 0 then return end

	local path = PathfindingService:CreatePath({
		AgentCanJump = true
	})

	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {player.Character, workspace.SpecialItems:GetDescendants(), monster, wp}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist

	local ray = workspace:Raycast(player.Character.PrimaryPart.Position, player.Character.PrimaryPart.CFrame.UpVector * -500, rayParams)

	if not ray then return end
	
	if ray.Instance.Name ~= "Baseplate" then print(ray.Instance.Name) end

	local endPos = ray.Position

	local success, err = pcall(function()
		path:ComputeAsync(monster.PrimaryPart.Position, endPos)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()

		if showWaypoints then
			if len(wp) > 0 then
				for i, v in pairs(wp) do
					v:Destroy()
				end
			end
			
			wp = {}

			for i, v in pairs(waypoints) do
				local part = Instance.new("Part")
				part.Position = v.Position
				part.Size = Vector3.new(0.5, 0.5, 0.5)
				part.CanCollide = false
				part.Anchored = true
				part.Name = "Waypoint"
				part.Parent = workspace
				table.insert(wp, part)
			end
		end

		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then
				blockedConnection:Disconnect()
				chasePlayer(player, monster, monsterSettings)
			end
		end)

		if not reachedConnection then
			reachedConnection = monster.Humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					local dist = (monster.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude

					if dist >= monsterSettings.KillDistance then
						nextWaypointIndex += 1
						monster.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					else
						player.Character.Humanoid:TakeDamage(100)
						reachedConnection:Disconnect()
						blockedConnection:Disconnect()
					end
				else
					player.Character.Humanoid:TakeDamage(100)
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		nextWaypointIndex = 2
		monster.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)

	else
		warn("Path not computed "..err)
	end
end

This is what triggers the ai chase and what happens after it stops

-- THE TASK.SPAWN IS IN A PART UNDER FUNCTION fireScript()

task.spawn(function()
	rsConnection = game:GetService("RunService").Stepped:Connect(function()
		if char.Humanoid.Health > 0 then
			chasing = true			
			if chasing then chasePlayer(player, monster, monsterSettings) end			
		else
			rsConnection:Disconnect()
			chasing = false
			cleanup(monster, grip, monsterSettings)
		end)
	end)
end

function cleanup(monster, grip, monsterSettings)	

	nextWaypointIndex = 2
	if blockedConnection then blockedConnection:Disconnect() blockedConnection = nil end
	if reachedConnection then reachedConnection:Disconnect() reachedConnection = nil end

	if rsConnection then rsConnection:Disconnect() rsConnection = nil end

	task.wait(monsterSettings.SleepTime)

	fireScript()
end

Any help is appreciated, I don’t know what’s causing it

You might have some network ownership issues going on (maybe you’re already doing this somewhere else).

1 Like

first issue I see is that you never set reachedConnection to nil after disconnecting it for this if condition to pass for the next chasePlayer(…)
this is probably the main issue for the npc not pathfinding properly except for the first time


another issue I see is that running out of waypoints before reaching the player will damage the player. this will work if the player doesnt move, but if the player can move, then the monster(?) can damage the player without being nearby


next, you should check when the path is blocked, if MoveToFinished triggers, or if Blocked triggers first. If MoveToFinished triggers first, it will disconnect BlockedConnection and make the noc stop moving

2 Likes

I did set networkownership to nil

1 Like

ill chrck it when i get home……

1 Like

Yeah I changed this, and now it kills which is great but there is one problem tho, ive tried testing out a print and it randomly stops working, ill send a video soon

Can you help me understand this more and how would I fix it?

And with this would i just do blockedConnection:Disconnect() as soon at movetofinished happens?

1 Like

I finally understood it and changed/fixed the code, thank you for your help <3

2 Likes

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