After encountering this issue myself I couldn’t find any solutions that worked for me by searching for it on the DevForum, methods such as setting the NPC Network Ownership did not work either, so after trying to figure out a solution myself I came up with this one and I wanted to share it with anyone that is having similar issues.
For this tutorial I’m using SimplePath, a great Pathfinding module which I am also using in the game I am working on.
This code is just an example of what to do, should work with the Roblox pathfinding with minimal changes
local isWalkPlaying = false -- Used to make sure the walk animation doesn't play on loop because Running fires many times with Pathfinding.
local isPathfinding = false -- Used to make sure that Pathfinding isn't running anymore and the script can continue to play the idle animation.
Humanoid.Running:Connect(function(speed) -- Listen for the Running event which returns the speed of the character.
isPathfinding = NPC:GetAttribute("Pathfinding") -- Get the current state of Pathfinding attribute.
if speed > 0 then -- If Speed is greater than 0 then the walk animation can continue.
if isWalkPlaying == false then -- Check if the walk animation is already playing.
runAnimation:Play(.2)
idleAnimation:Stop(.2)
isWalkPlaying = true
end
elseif speed <= 0 and isPathfinding == false then -- Checks if the NPC is still pathfinding/walking to the point, if not it plays the idle animation and stops the running animation.
idleAnimation:Play(.2)
runAnimation:Stop(.2)
isWalkPlaying = false
end
end)
Path.Reached:Connect(function() -- Detects when the NPC has reached the target position.
NPC:SetAttribute("Pathfinding", false) -- Sets pathfinding Attribute to false.
end)
NPC:SetAttribute("Pathfinding", true) -- Sets the Pathfinding attribute to true to prevent Idle animations from playing.
Path:Run(targetPosition) -- Pathfinding function from SimplePath.
What you need to do is add a “debounce” for the code, detect if the walk/run animation is playing or if is the NPC currently pathfinding
Place file with the whole code:
pathfindingExample.rbxl (77.8 KB)
Before:
After: