Pathfinding - Delay and sudden stops

  1. What do you want to achieve? My nextbot to chase the player without any delays and sudden stops.

  2. What is the issue? So I have a little nextbot game and the nextbot is delaying a bit and sometimes just suddenly stops. I feel like it’s delayed because the position of the player doesn’t get updated too frequently and the nextbot tries to reach the previous location of the player.

  3. What solutions have you tried so far? I’ve tried wrapping the path computing line in a while task.wait() do loop but that just stopped the rest of the code from running, even when I wrapped the while loop in a task.spawn() function.

I apologize for long code. I have tried to organize it just for the sake of readability and understandability.

Here’s also the game link so you guys could try it out real quick: old school we had but got demolished - Roblox

local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")

local NextbotHRP = script.Parent:WaitForChild("HumanoidRootPart")
local NextbotHumanoid = script.Parent:WaitForChild("Humanoid")

NextbotHRP.ChildAdded:Connect(function(Child)
	if Child:IsA("Sound") and Child.Name ~= "Idle Sound" then
		Child:Destroy()
	end
end)

-- Set the health of the nextbot's humanoid to infinite.
NextbotHumanoid.MaxHealth = math.huge
NextbotHumanoid.Health = NextbotHumanoid.MaxHealth

local ClosestHrp = nil
local Distance = nil
local MaxDistance = 500

local function CalculatePath()
	local Path = PathfindingService:CreatePath()
	
	Path:ComputeAsync(NextbotHRP.Position, ClosestHrp.Position)
	
	local Waypoints = Path:GetWaypoints()

	for _, waypoint in pairs(Waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			NextbotHumanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end

		NextbotHumanoid:MoveTo(waypoint.Position)
		NextbotHumanoid.MoveToFinished:Wait()
	end
end

while task.wait() do
	for _, player in ipairs(Players:GetPlayers()) do
		local Character = player.Character or player.CharacterAdded:Wait()
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		local Humanoid = Character:WaitForChild("Humanoid")
		if Humanoid.Health > 0 then
			local DistanceBetween = (HumanoidRootPart.Position - NextbotHRP.Position).Magnitude
			if Distance ~= nil then
				if DistanceBetween <= Distance and DistanceBetween < MaxDistance or #Players:GetPlayers() == 1 then
					Distance = DistanceBetween
					ClosestHrp = HumanoidRootPart
					CalculatePath()
				end
			else
				if DistanceBetween < MaxDistance then
					Distance = DistanceBetween
					ClosestHrp = HumanoidRootPart
					CalculatePath()
				end
			end
		end
	end
	
	NextbotHumanoid.Touched:Connect(function(hit)
		if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
			if Players:GetPlayerFromCharacter(hit.Parent) then
				hit.Parent:FindFirstChild("Humanoid").Health = 0
			end
		end
	end)
end

Set on all of the Nextbot’s parts the Network owner to nil (server) using SetNetworkOwner.

I’ve heard of that but I thought that was only for the primary part of the nextbot? (which is of course HumanoidRootPart)

For some reason not. As I said, use a for loop to set the network owner and try again.

Alright so I did what you said, looped through all the children of the nextbot and set the network owner of all of the parts of the nextbot to the server. I’m not sure about the sudden stops cause I tested it and they didn’t happen but the delays still happen. I’m sure there’s some kind of a loop needed in my code.

What exactly do you mean by delays? Does the Nextbot stop searching for a bit or…?

You should stop from using waiting functions, but instead rely on event based path iteration system.
This is what I mean

local CurrentProgressInEvent = ...
local EventCall = 0
function onEvent()
    EventCall += 1
    local CurrentEventCall = EventCall
    --logic if no then stop... 
    --You just keep track of all variables outside of the event chain and you stop when you need to.
    while CurrentEventCall == EventCall do
     wait()
     --another logic loop for an example for creating new paths (because players is moving so you have
 to update one)
      --also for the unreachable points problem resolution
    end
    --You do something here when the function ends
end

event:Connect(onEvent)

Well, it seems to me that the nextbot finds it’s way to the previous location of the player’s humanoid root part. So when the player moves like 100 studs away from one location, the nextbot will still try to reach the location that the player moved away from. You can test it out in the game linked in the topic’s description,

I don’t understand what you’re talking about but I’m sure I don’t need that, as my code is working properly without any errors (Except for the delays and possibly sudden stops if they will still occur at some point).

I just explained you how to resolve the problem with the sudden stops and how you can construct logic inside of the event chain…
(wait function is the reason why it does happen)

I just read another topic and according to this topic (which can be found here: AI unable to catch up to player - #60 by Superdestructo09), pathfinding on the server is much slower than on the client. It could possibly be the solution to the problem but I have no idea on how to implement it into my code. I want the other players to still be able to see the nextbot chasing other players and that could be changed if I would use a local script to do all the pathfinding.