I have a monster npc pathfinding script that walks to the player if it sees him and when it doesnt see it anymore (using raycasting) it walks back to its spawn location. The problem is that when it stops seeing the player and it walks back to the spawn position, the walking animation starts to stutter like in the video: (the animation doesnt stutter when walking towards the player)
robloxapp-20240128-1818065.wmv (489.7 KB)
Here’s the pathfinding script:
local PathfindingService = game:GetService(“PathfindingService”)
local RunService = game:GetService(“RunService”)
local npc = script.Parent
local humanoid = npc:WaitForChild(“Humanoid”)
local hrp = npc:WaitForChild(“HumanoidRootPart”)
hrp:SetNetworkOwner(nil)
local pathParams = {
AgentHeight = 0,
AgentRadius = 2,
AgentCanJump = false
}
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {npc}
local lastPos
local animPlaying = false
local RANGE = 40
local DAMAGE = 60
---- ANIMSSSSSSS -------
local attackanim = script:WaitForChild(“Attack”)
local attackanimtrack = humanoid:LoadAnimation(attackanim)
local function canSeeTarget(target)
local orgin = hrp.Position
local direction = (target.HumanoidRootPart.Position - hrp.Position).Unit * RANGE
local ray = workspace:Raycast(orgin, direction, rayParams)
if ray and ray.Instance then
if ray.Instance:IsDescendantOf(target) then
return true
else
return false
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = RANGE
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(hrp.Position - Vector3.new(0, npc.PrimaryPart.Size.Y/0.75, 0), destination.Position)
return path
end
local function attack(target)
local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
local debounce = false
if distance > 5 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
attackanimtrack:Stop()
else
if debounce == false then
debounce = true
attackanimtrack:Play()
wait(0.4)
target.Humanoid.Health -= DAMAGE
task.wait(2.1)
debounce = false
end
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(path:GetWaypoints()) do
path.Blocked:Connect(function()
path:Destroy()
end)
local target = findTarget()
if target and target.Humanoid.Health > 0 then
lastPos = target.HumanoidRootPart.Position
attack(target)
break
else
if lastPos then
humanoid:MoveTo(lastPos)
lastPos = nil
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
else
return
end
end
local function patrol()
local waypoints = game.Workspace.Waypoints.Part
walkTo(waypoints)
end
while true do
wait()
patrol()
end