What I want to achieve is a successful pathfinding for an NPC, that could not be pushed around, and is smooth.
I have tried going on wiki, but it is very glitchy, forexample, the NPC could be pushed, and it’s walking is very chunky/not smooth, and breaks most of the time.
Script: `local PathfindingService = game:GetService(“PathfindingService”)
– Variables for the zombie, its humanoid, and destination
local zombie = game.Workspace.Characters.Laila
local humanoid = zombie.Humanoid
local destination = script.Block
– Create the path object
local path = PathfindingService:CreatePath()
– Variables to store waypoints table and zombie’s current waypoint
local waypoints
local currentWaypointIndex
local function followPath(destinationObject)
– Compute and check the path
path:ComputeAsync(zombie.Head.Position, destinationObject.Position)
– Empty waypoints table after each new path computation
waypoints = {}
if path.Status == Enum.PathStatus.Success then
-- Get the path waypoints and start zombie walking
waypoints = path:GetWaypoints()
-- Move to first waypoint
currentWaypointIndex = 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
-- Error (path not found); stop humanoid
humanoid:MoveTo(zombie.HumanoidRootPart.Position)
end
end
local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex = currentWaypointIndex + 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
end
local function onPathBlocked(blockedWaypointIndex)
– Check if the obstacle is further down the path
if blockedWaypointIndex > currentWaypointIndex then
– Call function to re-compute the path
followPath(destination)
end
end
– Connect ‘Blocked’ event to the ‘onPathBlocked’ function
path.Blocked:Connect(onPathBlocked)
– Connect ‘MoveToFinished’ event to the ‘onWaypointReached’ function
humanoid.MoveToFinished:Connect(onWaypointReached)
followPath(destination)`