Really annoying pathfinding issue!

Hello, I’ve been having this issue recently with my pathfinding A.I that’s been bugging me for awhile. Basically, whenever the A.I gets close to the player, it starts moving really slow and goes to each waypoint one at a time making it really easy to avoid.

Here’s my code:

--Variables
local pathfindingService = game:GetService("PathfindingService")

--Dummy variables
local myHumanoid = script.Parent.Humanoid
local myTorso = script.Parent.HumanoidRootPart

--Configuration
local mag = script.Configuration.Magnitude
local view = script.Configuration.View
local check = script.Configuration.Check

--Variables
local foundPlayer

--Function that finds the closest player
function findPlayer()
	local view = view.Value --View distance of dummy
	--Look in the workspace for players
	for i, v in pairs(game.Workspace:GetChildren()) do
		local humanoid = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("UpperTorso")
		if humanoid and humanoid.Health > 0 and torso and v.Name ~= script.Parent.Name then
			--Check if player is in view
			if(myTorso.Position - torso.Position).magnitude < view and humanoid.Health ~= 0 then
				foundPlayer = torso --Set them to the foundPlayer
			end
		end
	end
end

--Function that creates a path to player
function createPath(player)
	--Create the path
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(myTorso.Position, player.Position)

	--Get the waypoints
	local waypoints = path:GetWaypoints()
	--Loop through each waypoint
	for i, waypoint in pairs(waypoints) do
		--Make dummy jump if neeeded
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			myHumanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		--Move to the closest waypoint
		myHumanoid:MoveTo(waypoint.Position)

		--Check if player moved
		if(myTorso.Position - waypoints[1].Position).magnitude > check.Value then
			createPath(player)
			break
		end

		--Detect if dummy has reached waypoint
		repeat wait(0.01)
			local distanceBetweenPoints = (waypoint.Position - myTorso.Position).magnitude
		until distanceBetweenPoints <= mag.Value
	end
end

--Function that finds a player, and creates a path to them
function main()	
	findPlayer()
	createPath(foundPlayer)
end

--Run all of the functions above
while wait() do
	main()
end
3 Likes

Try setting Network Ownership of the NPC to the server as it might be defaulting to the client.

You can find more info about Network Ownership here:

1 Like

Which part should I do that to? Should I make a for loop for all of the parts / limbs, or just a specific one?

Yeah, you can just loop through the NPC model and set the ownership to nil.

Like so:

for _, Part in pairs(NPC:GetDescendants())do
	if Part:IsA("BasePart") then
		if Part:CanSetNetworkOwnership() then
			Part:SetNetworkOwner();
		end;
	end;
end;

Might be a typo so be cautious of that.

1 Like

I know my answer sounds stupid but, you can try directly moving it when the magnitude is close.

That seems to have fixed it, thanks!