Animations are stuttering whenever my NPC moves

Hello, I recently followed a tutorial made by GnomeCode, but the animations stutter for some reason, I think it has something to do with the AI script possibly:

Here is my code for both the scripts:

AI Script:

local NPC = script.Parent
local humanoid = NPC.Humanoid
NPC.PrimaryPart:SetNetworkOwner(nil)

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")

	local pathParams = {
		["AgentHeight"] = 8,
		["AgentRadius"] = 8,
		["AgentCanJump"] = true
	}

	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(NPC.HumanoidRootPart.Position, destination.Position)

	return path
end

local function displayPath(waypoints)
	local color = BrickColor.Random()
	for index, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.BrickColor = color
		part.Anchored = true
		part.CanCollide = false
		part.Size = Vector3.new(1,1,1)
		part.Position = waypoint.Position
		part.Parent = workspace
		local Debris = game:GetService("Debris")
		Debris:AddItem(part, 6)
	end
end

local function walkTo(destination)

	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		--displayPath(path:GetWaypoints())
		for index, waypoint in pairs(path:GetWaypoints()) do
			--print("Moving to ", waypoint.Position)
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
end

function patrol()
	local waypoints = workspace.PathfindingSpots:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.25) do
	patrol()
end

Animation Script:

local NPC = script.Parent

local Animator = NPC.Humanoid:WaitForChild("Animator")

local Humanoid = NPC:WaitForChild("Humanoid")

local WalkAnim = Animator:LoadAnimation(script:WaitForChild("WalkAnim"))

Humanoid.Running:Connect(function(speed)
	if speed > 0 then
		WalkAnim:Play(0.5)
	else
		WalkAnim:Stop(0.5)
	end
end)
3 Likes

just something to try, have you tried to not play the walk animation, if its already playing (hasnt been stopped) ?

1 Like

Nope, it still stutters, here is my animation code:

local NPC = script.Parent

local Animator = NPC.Humanoid:WaitForChild("Animator")

local Humanoid = NPC:WaitForChild("Humanoid")

local WalkAnim = Animator:LoadAnimation(script:WaitForChild("WalkAnim"))

Humanoid.Running:Connect(function(speed)
	if speed > 0 and not WalkAnim.IsPlaying then
		WalkAnim:Play(0.5)
	else
		WalkAnim:Stop(0.5)
	end
end)
1 Like
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local walkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		if not walkAnimTrack.IsPlaying then
			walkAnimTrack:Play()
		end
	else
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end
	end
end)

image
image
Then upload you animation and put it into the walk animationId

1 Like

The walk animation is still stuttering though, it could have something to do with the AI script, possibly.

Can i see the AI Script that you made?

Sure, here it is:

local NPC = script.Parent
local humanoid = NPC.Humanoid
NPC.PrimaryPart:SetNetworkOwner(nil)

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")

	local pathParams = {
		["AgentHeight"] = 8,
		["AgentRadius"] = 8,
		["AgentCanJump"] = true
	}

	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(NPC.HumanoidRootPart.Position, destination.Position)

	return path
end

local function displayPath(waypoints)
	local color = BrickColor.Random()
	for index, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.BrickColor = color
		part.Anchored = true
		part.CanCollide = false
		part.Size = Vector3.new(1,1,1)
		part.Position = waypoint.Position
		part.Parent = workspace
		local Debris = game:GetService("Debris")
		Debris:AddItem(part, 6)
	end
end

local function walkTo(destination)

	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		--displayPath(path:GetWaypoints())
		for index, waypoint in pairs(path:GetWaypoints()) do
			--print("Moving to ", waypoint.Position)
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
end

function patrol()
	local waypoints = workspace.PathfindingSpots:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.25) do
	patrol()
end

Im not sure whats wrong everything looks right can you send me a video?

I actually tested it on a normal dummy and it worked fine, it might be something with the NPC rig I’m using, I’ll go ahead and fix it, thanks for your help.

2 Likes