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
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)
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
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.
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
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.