-- 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.Position, destinationObject)
	-- 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
		human:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		-- Error (path not found); stop humanoid
		human:MoveTo(zombie.Position)
	end
end
 
local function onWaypointReached(reached)
	if reached and waypoints ~= nil and currentWaypointIndex ~= nil and currentWaypointIndex < #waypoints then
		currentWaypointIndex = currentWaypointIndex + 1
		human: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
 