Pathfinding Modifiers Not Working?

The NPC Moves through/into the Zone we have designated as math.huge… No idea what is wrong with it. I know the code is very messy but yea, if got any suggestions…

Zones
image

PathfindingModifier
image

SafezonePart
image

The Full Code:

local animation1 = script:WaitForChild('Normal')
local animation2 = script:WaitForChild("Jump")
local animation3 = script:WaitForChild("Run")
local humanoid = script.Parent:WaitForChild('Humanoid')
local walk = humanoid:LoadAnimation(animation1)
local run = humanoid:LoadAnimation(animation3)
local jump = humanoid:LoadAnimation(animation2)
local pathfinding = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local IE = RS.Events.InteractEvent
local PS = game:GetService("Players")


local rootPart = script.Parent:WaitForChild("HumanoidRootPart") or script.Parent:WaitForChild("Torso")
local humanoid = script.Parent:WaitForChild("Humanoid")
local destination = false

local cd = false
local scd = false
local scc = 0
local sca = 30
local countcd = 0


function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 1500
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart") or temp2:FindFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end

			return torso
end


function hit(destination,rootPart)
	local player = PS:GetPlayerFromCharacter(destination.Parent)
	if player then
	local targetp = rootPart.CFrame.LookVector * 5000
		IE:FireClient(player, "RIP",targetp)
		end
	destination.Parent.Humanoid.Health = destination.Parent.Humanoid.Health - 50
	
end

wait()

rootPart:SetNetworkOwner(nil)

while true == true do
	
	wait(1)
	local destination = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	
	if destination ~= nil then
		local path = pathfinding:CreatePath({
			AgentCanJump = false,
			Costs = {
					Safezone = math.huge
			}
		})
		path:ComputeAsync(rootPart.Position,destination.Position)
		local wps = path:GetWaypoints()
		for i,v in pairs(wps) do
		
			
			if v.Action == Enum.PathWaypointAction.Jump then
				jump:Play()
				script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				local jumping = true
				while humanoid.Jumping do
					wait(0.1)
				end
			else
				
		
			if (destination.Position - script.Parent.HumanoidRootPart.Position).magnitude < 10 then
				hit(destination,rootPart)
				break
				elseif (destination.Position - script.Parent.HumanoidRootPart.Position).magnitude < 150	then
					
				if path:CheckOcclusionAsync(i) == -1 and path.Status == Enum.PathStatus.Success then
					humanoid:MoveTo(destination.Position)
					if scd == false then
						script.Parent.Jumpscare2:Play()
						scd = true
					else
						if scc >= sca then
							scc = 0
							scd = false
						else
							scc = scc + 1
						end
					end
					run:Play()
					humanoid.WalkSpeed = 30
				else
				end
			else
				humanoid:MoveTo(v.Position)
				if not cd then 
					cd = true
				if (destination.Position - script.Parent.HumanoidRootPart.Position).magnitude < 300 then
						if scd == false then
							script.Parent.Jumpscare2:Play()
							scd = true
						else
							if scc >= sca then
								scc = 0
								scd = false
							else
								scc = scc + 1
							end
						end
					run:Play()
					humanoid.WalkSpeed = 30
				else
					walk:Play()
					humanoid.WalkSpeed = 10
				end
			else
					if countcd > ((destination.Position - script.Parent.HumanoidRootPart.Position).magnitude / 20 )  then
						cd = false
						countcd = 0
						break
						else
						countcd = countcd + 1
					end
				end
			end
			humanoid.MoveToFinished:Wait()
			end
		end
		jump:Stop()
		run:Stop()
		walk:Stop()
	end
	
	
end
1 Like

Sorry for the bump, but have you found a solution to this? I’m having the same issue.

1 Like

Are you trying to get the NPC to go into the safezone or not? I’m a little confused.

Do you not know how pathfinding costs work? If so, please don’t try helping with something about them.

Anyways, he’s trying to make the agent avoid the safe zone. The higher the pathfinding cost, the harder it is to travel through/over it. math.huge means infinity, which should make it impossible to pathfinding through, as far as I know.

1 Like

I think the problem has something to do with your usage of the moveto function. The path may be setting up correctly, but your indicating the humanoid of the npc to move to the destination, not the next waypoint. In this way, the npc is completely disregarding the path you created and moving directly towards the destination.

Use the humanoid:MoveTo function something like this:

humanoid:MoveTo(wps[currentWaypoint].Position)

Let me know if you need more help or if you fixed it.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.