Why isn't this yielding?

Hi.
I am working on a pet system.

This is the code;

Pet = Pet:Clone()
		Pet.Parent = workspace.Programming.Pets
		Pet.Name = Player.Name
		local lt = Player.Character.HumanoidRootPart.Position
		while wait() do
			PFS.MoveHumanoid(Pet,Player.Character.HumanoidRootPart.Position)
		end

I mean, just a while loop to move the pet to the humanoid positions,
But obviously, it glitches and it PFS.MoveHumanoid Doesn’t yields

but it should yield

This happens;

Obviously, because it’s looping a lot of times and it moves to waypoints a LOT of times.
So, why isn’t the module yielding?
Module code;

local PathFinding = game:GetService('PathfindingService')
local module = {}

function module.MoveHumanoid(npc,destination)
	local startPosition = npc.Torso.Position
	local EndPosition = destination
	local path = PathFinding:CreatePath()

	local waypoints 
	local currentWaypointIndex = 1

	local function followPath(destinationObject)
		path:ComputeAsync(startPosition, destinationObject)
		waypoints = {}

		if path.Status == Enum.PathStatus.Success then
			waypoints = path:GetWaypoints()
			currentWaypointIndex = 1
			npc.Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
		else
			npc.Humanoid:MoveTo(npc.Torso.Position)
		end
	end

	local function onWaypointReached(reached)
		if (not currentWaypointIndex) then
			return
		end
		if reached and currentWaypointIndex < #waypoints then
			currentWaypointIndex = currentWaypointIndex + 1
			npc.Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
		end
	end

	local function onPathBlocked(blockedWaypointIndex)
		if blockedWaypointIndex > currentWaypointIndex then
			followPath(destination)
		end
	end

	path.Blocked:Connect(onPathBlocked)

	npc.Humanoid.MoveToFinished:Connect(onWaypointReached)

	followPath(destination)
end


return module

Is there a better way to make the NPC follows at all cost the player without this glitch

1 Like

You are using PathfindingService incorrectly. You were supposed to create a path and update it instead of always creating a new one.

It would require a complete rewrite of the way you handle this, but this is the general concept.

local servicePathfinding = game:GetService("PathfindingService")
local serviceRun = game:GetService("RunService")

local npcpath = servicePathfinding:CreatePath({
	AgentRadius = 0.5,
	AgentHeight = 2,
	AgentCanJump = true,
})

local npctorso = npc.Torso
local charhrp = Player.Character.HumanoidRootPart
local npchumanoid = npc.Humanoid

local lastcharpos = Vector3.new()

serviceRun.Heartbeat:Connect(function()
	local npcpos = npctorso.Position
	local charpos = charhrp.Position

	if (lastcharpos-charpos).Magnitude<0.5 then return end
	if (npcpos-charpos).Magnitude<2 then return end

	lastcharpos = charpos

	npcpath:ComputeAsync(npctorso.Position, charhrp.Position)
	local target
	if npcpath.Success == Enum.PathStatus.Success then
		local waypoints = npcpath:GetWaypoints()
		target = waypoints[1]
	else
		target = npcpos
	end

	npchumanoid.WalkToPoint = target

end)
1 Like

Hi, I am having a trouble.

while Pet do
			local npcpos = Pet.Torso.Position
			local charpos = Player.Character.HumanoidRootPart.Position

			if (lastcharpos-charpos).Magnitude<0.5 then return end
			if (npcpos-charpos).Magnitude<2 then return end

			lastcharpos = charpos

			npcpath:ComputeAsync(Pet.Torso.Position, Player.Character.HumanoidRootPart.Position)
			local target
			if npcpath.Status == Enum.PathStatus.Success then
				local waypoints = npcpath:GetWaypoints()
				target = waypoints[1].Position
			else
				target = npcpos
			end

			Pet.Humanoid.WalkToPoint = target
		end

So the pet doesn’t moves at all.
I edited a bit the code since there were some small mistakes

The while pet part is weird. I don’t know what it’s supposed to do, but I’m surprised it doesn’t crash your game.

You could try using :MoveTo instead of .WalkToPoint. It’s 4am for me, so I sadly won’t be able to reply.

Apologies, didn’t knew your timezone, simes like while Pet do
isn’t even running otherwise, would crash, thanks!!
nvm, just realized
“Pet” is returning nil, so it’s not your code haha.

wait, no, somethins is yielding at the code wth…

Try replacing waypoints[1] with waypoints[2].

1 Like

It works a bit, but then it stops moving, like it gets stuck


Like if it wasn’t smart.
And it’s really to get stuck

This is code;

while (true) do
			wait()
			if (not Pet) then
				break
			end
			coroutine.wrap(function()
			local npcpos = Pet.Torso.Position
			local charpos = Player.Character.HumanoidRootPart.Position
				if (charpos - npcpos).Magnitude < 2 then
					
					return
				end
			npcpath:ComputeAsync(Pet.Torso.Position, Player.Character.HumanoidRootPart.Position)
			local target
			if npcpath.Status == Enum.PathStatus.Success then
					local waypoints = npcpath:GetWaypoints()
				target = waypoints[2].Position
				else
				target = npcpos
			end
				Pet.Humanoid:MoveTo(target)
				end)()
		end

Do you think you can help me?/

Try setting AgentCanJump to false. It looks like it thinks it can jump over that tree’s root. Also, you might have to add a teleport fix in case it gets too far from the player.

Hm, I setted it to false, but seems like it makes the same, maybe it thinks the agent it’s too big i’ll try reducing AgentHeight?

Hm nope, it seems like it considers the bench stairs and it climbs

Found the issue.

target = npcpos

has to be replaced with

target = charpos

Also, you don’t need the coroutine or the wait. ComputeAsync already yields the code and the compute calls won’t pile up if you remove the coroutine.

1 Like