Pathfinding AI stutter while following a player

hey, I’m trying to make my own “The Rake” game. I have issues with pathfinding though. the AI will start stuttering while it’s trying to chase the player. I’ve tried to draw the paths and… the waypoints are a LOT. here’s a video:

the AI walks normally if it’s wandering, but goes crazy while chasing. so I have no idea what’s the issue. here’s the code:

local function findNearestTarget()
	local nearestMagnitude = math.huge
	local nearestTarget

	for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
		if plr.Character then
			local char = plr.Character
			local targetHum = char:WaitForChild("Humanoid")
			if (char.HumanoidRootPart.Position - hrp.Position).Magnitude < dist.Value then
				if targetHum.Health > 0 and nearestMag >= (char.HumanoidRootPart.Position - hrp.Position).Magnitude then
					nearestMag = (char.HumanoidRootPart.Position - hrp.Position).Magnitude
					nearestTarget = char
					print("found target")
				end
			end
		end
	end
	return nearestTarget
end

local function followTarget(target)
	local path = pf:CreatePath()
	path:ComputeAsync(hrp.Position, target.HumanoidRootPart.Position)
	local waypoint = path:GetWaypoints()

	for i, v in pairs(waypoint) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			hum:ChangeState(Enum.HumanoidStateType.Jumping)
		end

		local part = drawPath(v)
		if sGoingToCave.Value or not chasing or sChasing.Value == false then part:Destroy() break end

		hum:MoveTo(v.Position)
		hum.MoveToFinished:Wait()
		part:Destroy()
	end
end

while true do
	game:GetService("RunService").Heartbeat:Wait()
	local target = findNearestTarget()
	if target then
		sChasing.Value = true
		if not chasing then chasing = true
			if not screamCD then screamCD = true
				scream("Found")
				coroutine.wrap(function()
					task.wait(2)
					screamCD = false
				end)()
			end
		else
			if not blinded.Value then
				hum.WalkSpeed = runSpeed.Value
			else
				hum.WalkSpeed = 0
			end
		end

		local ray = Ray.new(hrp.Position, (target.HumanoidRootPart.Position - hrp.Position).Unit * 500)
		local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {rake})

		coroutine.wrap(function()
			if (target.HumanoidRootPart.Position - hrp.Position).Magnitude < 7 then 
				if not attackCD and not blinded.Value then attackCD = true
					attackTarget(target)
				end
			end
		end)()

		coroutine.wrap(function()
			if hit then
				if hit:IsDescendantOf(target) then
					followTarget(target)
				end
			end
		end)()
	end
end

if it isn’t clear enough then do tell me, thanks!

1 Like

You might be able to fix it with this:
(Make sure its the Killers HumanoidRootPart)

hrp:SetNetworkOwner(nil)

Edit: i noticed it at the beginning of the script, make sure it follows the Players Humanoid and not a Rigs Humanoid

1 Like

From reading the code I personally cannot see an extremely obvious issue, perhaps make sure the AI is not trying to wander while also chasing a player.

Generally speaking pathfinding is expensive, so if the AI has: a clear line of sight, is close by to the target, and is at a similar height to the target, I wouldn’t bother pathfinding and simply have it walk directly towards the target.

Also there is no need to have a custom function to draw your AI’s path, as I believe there is an inhouse option for this somewhere in the studios rendering settings.

2 Likes

yes, I’ve done that. I stopped the loop when the AI is chasing. I may be doing it wrong but here’s what I did:

	if path.Status == Enum.PathStatus.Success then
		for i, v in pairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				hum:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			
			local part = drawPath(v)
			if sGoingToCave.Value 
				or chasing 
				or sChasing.Value == true 
				or findNearestTarget() then part:Destroy() break 
			end

			hum:MoveTo(v.Position)
			hum.MoveToFinished:Wait()
			part:Destroy()
		end
	end

the reason why I used pathfinding is to make the AI jump when required. I don’t think I can make it jump if I just make it walk directly towards the player, but I might be wrong

oh really? I didn’t know that. May I know where that setting is? I can’t seem to find it.

1 Like

I’ve done that already, and sadly wasn’t the solution

1 Like

File → Studio Setting’s → Studio → Show Navigation Labels
(Shown in yellow, only on server view however)

1 Like

bumping this, I still need help with this

It looks like your while loop is continually adding more paths for the npc to follow without canceling the previous order. Resulting in it trying to follow multiple paths at once.

found the solution, I just have to remove the coroutine.wrap() and it fixes the issue. I also removed hum.MoveToFinished:Wait() inside followTarget() function and everything works as expected. Thank you all for the help!

2 Likes

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