basically, i have an NPC and the problem is it Stutter-steps. The stutter stepping mostly occurs after its Target gets killed, or the stutter stepping is sometimes random.
what i mean by stutterstepping is walking, then stopping, then walking, and so on
vid of stutter step:
heres the code:
local pathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local foundtarget = nil
local targetpos = nil
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local body = script.Parent:FindFirstChild("Torso")
local destination = game.Workspace.Path:GetChildren()
local IdleAnim = humanoid.Animator:LoadAnimation(script.Parent.Idle)
local WalkAnim = humanoid.Animator:LoadAnimation(script.Parent.Walk)
local RunAnim = humanoid.Animator:LoadAnimation(script.Parent.Run)
local blockedConnection
for _, desc in pairs(body:GetDescendants()) do
if (desc:IsA("BasePart")) then
desc:SetNetworkOwner(nil)
end
end
humanoidrootpart:SetNetworkOwner(nil)
repeat task.wait(1) until humanoidrootpart.Anchored == false
humanoidrootpart:SetNetworkOwner(nil)
local points = {}
for _,part in ipairs(destination) do
table.insert(points,part)
wait()
end
foundtarget = nil
function walkRandomly()
foundtarget = nil
local point = points[math.random(1,#points)]
local target = FindTarget()
local path = game:GetService("PathfindingService"):CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = true
})
path:ComputeAsync(humanoidrootpart.Position, point.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
if foundtarget == nil then
print("DEBUG: WALKING RANDOMLY TO WAYPOINT")
humanoid:MoveTo(waypoint.Position)
path.Blocked:Connect(function()
print("DEBUG: PATH BLOCKED (WALKRANDOMLY)")
main()
end)
if waypoint.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
if foundtarget == 1 then path:Destroy() break end
humanoid.MoveToFinished:Wait()
FindTarget()
end
end
--[[
local path = game:GetService("PathfindingService"):CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false
})
--]]
function findPath(target)
local path = game:GetService("PathfindingService"):CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = true
})
path:ComputeAsync(humanoidrootpart.Position,target.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
if checkSight(target) == true then
repeat
humanoid:MoveTo(target.Position)
path.Blocked:Connect(function()
print("DEBUG: PATH BLOCKED (findPath")
main()
end)
task.wait()
if target == nil then
break
elseif target.Parent == nil then
break
end
until checkSight(target) == false
print("DEBUG: LOST TARGET, DESTROYING PATH (findPath)")
foundtarget = nil
path:Destroy()
break
end
if (humanoidrootpart.Position - waypoints[1].Position).Magnitude > 30 then
print("DEBUG: FINDPATH(TARGET) (findPath)")
findPath(target)
break
end
end
end
function FindTarget()
local dist = 40
local target
local players = game.Players:GetPlayers()
for i,v in ipairs(players) do
if (v.Character) then
local torso = v.Character.Torso
local human = v.Character.Humanoid
if (humanoidrootpart.Position - torso.Position).Magnitude < dist and human.Health > 0 then
if checkSight(torso) and human.Health ~= 0 then
print("DEBUG: TARGET = TORSO (FindTarget)")
print(human.Health)
target = torso
end
end
end
end
return target
end
function checkSight(target)
local ray = Ray.new(humanoidrootpart.Position, (target.Position - humanoidrootpart.Position).Unit * 50)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
if hit then
if hit:IsDescendantOf(target.Parent) then
print("DEBUG: FOUND TARGET (checkSight)")
foundtarget = 1
return true
end
end
return false
end
function main()
local target = FindTarget()
if target and foundtarget == 1 then
print(target)
local Player = game.Players:GetPlayerFromCharacter(target.Parent)
humanoid.WalkSpeed = 25
findPath(target)
else
foundtarget = nil
humanoid.WalkSpeed = 11
walkRandomly()
end
end
humanoid.Running:Connect(function(speed)
if speed > 17 then
IdleAnim:Stop()
WalkAnim:Stop()
RunAnim:Play()
elseif speed > 4 and speed < 17 then
IdleAnim:Stop()
WalkAnim:Play()
RunAnim:Stop()
elseif speed <= 4 then
IdleAnim:Play()
WalkAnim:Stop()
RunAnim:Stop()
end
end)
while RunService.Heartbeat:Wait() do
main()
end