Creating new path and abandoning old path

Hello,
I want my NPC to stop following the old path and follow the new path.

The player alternates from the waypoints of the first path to the second path.

I tried using the path.Blocked function but that didn’t work so I switched to raycasting for when the ray hits a wall it creates a new path but the old path is there so the NPC alternates from the waypoint of the first path the to the second. I tried destroying the first path, and its waypoints but that didn’t work.

This is the code for the path and the ray

for i,v in pairs(EnemyNPC:GetChildren()) do
	local nhum = goku:FindFirstChild("Humanoid")
	local nhumroot = goku:WaitForChild("HumanoidRootPart")
	local ehum = v:FindFirstChild("Humanoid")
	local ehumroot = v:WaitForChild("HumanoidRootPart")
	local path = PS:CreatePath()
	
	path:ComputeAsync(nhumroot.Position, ehumroot.Position)
	local Waypoints = path:GetWaypoints()
	
	local function NewPath()
		local path2 = PS:CreatePath()
		path2:ComputeAsync(nhumroot.Position, ehumroot.Position)
		local Waypoints = path2:GetWaypoints()

		for i, waypoint2 in pairs(Waypoints)do
			local part = Instance.new("Part")
			part.Material = "Neon"
			part.Shape = "Ball"
			part.Size = Vector3.new(0.6,0.6,0.6)
			part.Position = waypoint2.Position + Vector3.new(0,2,0)
			part.Anchored = true
			part.CanCollide = false
			part.Parent = game.Workspace
			
			nhum:MoveTo(waypoint2.Position)
			nhum.MoveToFinished:Wait()
		end
	end
	
	RS.Heartbeat:Connect(function()

		local rayParam = RaycastParams.new()
		rayParam.FilterDescendantsInstances = {v, goku:GetChildren()}
		rayParam.FilterType = Enum.RaycastFilterType.Blacklist
		local ray = game.Workspace:Raycast(nhumroot.Position,nhumroot.CFrame.lookVector*10, rayParam)        
		if ray and CanHit then
			CanHit = false
			print(ray.Instance:GetFullName())
			NewPath()
		end
	end)


like this

2 Likes

Thanks, it works now. If I wanted to do it for the new path and paths after that, would I put it in a loop?