I have some NPCs which walk around in my game. But they’re walk animations are glitching.
I’m just using the default animation script that is also used by the Player.
Any ideas on why this is happening?
Hear is the code for my pathfinding
local function moveNPCToWaypoints(npcPart, npcPathFolder)
local waypoints = npcPathFolder:GetChildren()
local nextWayPointIndex
local reachedConnection
local blockedConnection
local currentWaypointIndex = 1
local Walkspeed = npcPart:GetAttribute("Walkspeed")
local humanoid = npcPart:FindFirstChild("Humanoid")
humanoid.WalkSpeed = Walkspeed
npcPart.PrimaryPart:SetNetworkOwner(nil)
local function getPath(destination)
local path = Pathfinding:CreatePath({
AgentHeight = 3;
AgentRadius = 2.25;
AgentCanJump = false;
AgentCanClimb = false;
Consts = {
Waiter = 100;
DangerZoner = math.huge
}
})
if npcPart ~= nil and npcPart:FindFirstChild("HumanoidRootPart") then
path:ComputeAsync(npcPart.HumanoidRootPart.Position, destination.Position)
end
return path
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
if npcPart ~= nil and npcPart:FindFirstChild("HumanoidRootPart") then
humanoid:MoveTo(destination.Position - (npcPart.HumanoidRootPart.CFrame.LookVector * 10))
end
end
end
local function Patrol()
local currentWaypoint = waypoints[currentWaypointIndex]
walkTo(currentWaypoint)
if currentWaypoint:FindFirstChild("WaitTime") then
local WaitTimeValue = currentWaypoint.WaitTime.Value
wait(WaitTimeValue)
end
currentWaypointIndex = currentWaypointIndex + 1
if currentWaypointIndex > #waypoints then
currentWaypointIndex = 1 -- Reset back to the first waypoint
end
end
while wait(.01) do
Patrol()
end
end