Path blocked then precompute path

I’m trying to make if a path is blocked, the NPC will calculate a new path and go to that path

It prints but it goes the same path
image

script:

local function onPathBlocked()
	print("why")
	
	waypoints = {} -- destroys/empties waypoints
	path:ComputeAsync(torse.Position, game.Workspace.End.Position) -- creates new path
	waypoints = path:GetWaypoints() -- gets new path's waypoints
end

path.Blocked:Connect(onPathBlocked)

full script:


local PathFindingService = game:GetService("PathfindingService") 
local human = script.Parent:WaitForChild("Humanoid")
local torse = script.Parent:WaitForChild("Torso")

local modifierVolume = game.Workspace.crosswalk

local modifier = Instance.new("PathfindingModifier", modifierVolume)
	modifier.Label = "All"

local agentParameters = {
	Costs = {
		All = 100

 	}
}


local path = PathFindingService:CreatePath(agentParameters)
 path:ComputeAsync(torse.Position, game.Workspace.End.Position)
  local waypoints = path:GetWaypoints()



wait(0.1)
for i, waypoint in pairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
		human:ChangeState(Enum.HumanoidStateType.Jumping)	
	end
	
	human:MoveTo(waypoint.Position)
	human.MoveToFinished:Wait(1)
end
wait()
human:MoveTo(game.Workspace.End.Position)
script.Parent:Destroy()

local function onPathBlocked()
	print("why")
	
	waypoints = {} -- destroys/empties waypoints
	path:ComputeAsync(torse.Position, game.Workspace.End.Position) -- creates new path
	waypoints = path:GetWaypoints() -- gets new paths waypoints
end

path.Blocked:Connect(onPathBlocked)
1 Like