Pathfinding NPC delay issue

Hi! I’m currently trying to make this pathfinding npc not have a delay when “surrounding” the player.

Basically, the target (for testing purposes this target is the player at the moment) has a cylinder welded to it’s HumanoidRootPart and the npc keeps circling around random areas right outside of the cylinder.

Here’s the script, I need it to explain where I think the problem is located:

local Path
local PreviousWaypoint
local Part = workspace.Part -- The cylinder that is welded to the player
script.Parent.PrimaryPart:SetNetworkOwner(nil)

local function GetPath(destination)
	Path = game:GetService("PathfindingService"):CreatePath({WaypointSpacing = math.huge, Costs = {TargetCylinder = math.huge}}) -- the cylinder has a PathfindingModifier labeled "TargetCylinder"
	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination)
end
while wait() do
	local SetupWaypointCFrame = CFrame.new(Part.Position) * CFrame.Angles(0, math.random(0, 360), 0)
	local WaypointCFrame = SetupWaypointCFrame + (SetupWaypointCFrame.LookVector * Vector3.new(Part.Size.Z - 5, 0, Part.Size.Z - 5))
	GetPath(WaypointCFrame.Position)
	for i, v in pairs(Path:GetWaypoints()) do
		local GreatHeight = false
		--[[if PreviousWaypoint ~= nil then --IRRELEVANT FOR MY PROBLEM
			if PreviousWaypoint.Position.Y - v.Position.Y >= 20 then
				GreatHeight = true
			end
		end]]--
		PreviousWaypoint = v
		if not GreatHeight then
			script.Parent.Humanoid:MoveTo(v.Position)
			script.Parent.Humanoid.MoveToFinished:Wait()
		else
			break
		end
	end
end

due to “script.Parent.Humanoid.MoveToFinished:Wait()”, the npc will always move to the next waypoint, no matter if it’s an outdated one. How can I break from this entire code and prevent it from waiting to complete the MoveTo when the cylinder/target moves?

2 Likes

And of course I forget to showcase this with a video, perfect…

(Note how the NPC goes to my previous locations due to it’s waypoint being way over there, hence, outdated.)

3 Likes

Gonna test out my code some more by removing the MoveTo. If anyone has some thoughts about this, please share them, this issue is very annoying.

1 Like

Is the NPC supposed to circle around the player or is it supposed to stop near it?

1 Like

It’s supposed to circle around the target. Best example I can get of this is probably Dummies Vs Noobs’s NPCs

I’ve been trying for quite a while and couldn’t find a solution, the best solution I thought of was to only run MoveToFinished:Wait() if the player’s position didn’t change, but that wasn’t very good at all.

Don’t worry, I found a bit of a solution to this but it still has some problems. This post helped me a lot.

Here’s what I’ve done:

local Path
local PreviousWaypoint
local Part = workspace.Part
local Moving = false
script.Parent.PrimaryPart:SetNetworkOwner(nil)
local function GetPath(destination)
	Path = game:GetService("PathfindingService"):CreatePath({WaypointSpacing = math.huge, Costs = {TargetCylinder = math.huge}})
	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination)
end
while wait() do
	local TargetCFrame = workspace.Part.CFrame
	local SetupWaypointCFrame = CFrame.new(Part.Position) * CFrame.Angles(0, math.random(0, 360), 0)
	local WaypointCFrame = SetupWaypointCFrame + (SetupWaypointCFrame.LookVector * Vector3.new(Part.Size.Z - 5, 0, Part.Size.Z - 5))
	GetPath(WaypointCFrame.Position)
	for i, v in pairs(Path:GetWaypoints()) do
		--[[local GreatHeight = false --IRRELEVANT FOR MY PROBLEM
		if PreviousWaypoint ~= nil then
			if PreviousWaypoint.Position.Y - v.Position.Y >= 20 then
				GreatHeight = true
			end
		end
		PreviousWaypoint = v]]--
		if not GreatHeight then
			script.Parent.Humanoid:MoveTo(v.Position)
			Moving = true
			repeat wait()
				script.Parent.MovingEvent.Event:Connect(function(bool)
					Moving = bool
				end)
			until TargetCFrame ~= workspace.Part.CFrame or Moving == false
		else
			break
		end
	end
end

And I set up an auxiliary script too:

script.Parent.Humanoid.MoveToFinished:Connect(function()
	script.Parent.MovingEvent:Fire(false)
end)

Although it seems to uh… See for yourself:


(It’s very undecisive when it’s not around the cylinder)

Good part is that I think I have a solution for that, which is making the ai follow the player itself instead of waypoints if there’s a certain distance between player and NPC and line of sight is established, bad part is that I’m pretty sure that’ll take some complex coding (or I could be wrong, we’ll see).

Good progress although I don’t really think it’s a good idea to use events for this since I think it can be handled in a single script.

I’m not really sure if it can be handled in a single script due to the while wait() do and repeat until loops…

EDIT : Nevermind, I see what you mean.

image
Your WaypointSpacing is set to math.huge, which creates only 2 waypoints; start and end. This makes pathfinding service useless, since you can achieve the same thing by only :MoveTo(WaypointCFrame.Position). Are you sure this is intentional? If so, just do :MoveTo in a loop to the destination.

2 Likes

Just noticed and changed that some seconds ago lol. The only reason I did that is because I wasn’t really sure of what to set so I just changed it back to default. That and making sure the first waypoint never plays fixed the undecisive NPC problem, thank you!

2 Likes

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