Problem's with Npc Pathfinding script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to get an understanding on why my pathfinding script is not working

  2. What is the issue? Include screenshots / videos if possible! My Npc Pathfinding script with magnitude is not working

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I spent a good hour debuging the script but still not luck

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Dummy = script.Parent
local PathFindingService = game:GetService("PathfindingService")

local function FindNearestTarget()
	local MaxDistance = 100
	local Target = nil
	for _,v in ipairs(workspace:GetChildren()) do
		local Humanoid = v:FindFirstChild("Humanoid")
		local HumanoidRootPart = v:FindFirstChild("HumanoidRootPart")
		if Humanoid and HumanoidRootPart and v ~= script.Parent then
			if (Dummy.HumanoidRootPart.Position - HumanoidRootPart.Position).Magnitude > MaxDistance then
				MaxDistance = (Dummy.HumanoidRootPart.Position - HumanoidRootPart.Position).Magnitude
				Target = HumanoidRootPart
			end
		end
		return Humanoid
	end
	return Target
end

local function RunLoopWithoutProblems()
	while task.wait(1) do
		local ReturnedTarget = FindNearestTarget()
		if ReturnedTarget then
			local Path = PathFindingService:CreatePath()
			local ReturnedHumanoidRootPart = FindNearestTarget()
			Path:ComputeAsync(Dummy.Humanoid.RootPart.Position, ReturnedHumanoidRootPart.Position)
			if Path.Status == Enum.PathStatus.Success then
				local Waypoints = Path:GetWaypoints()
				for _,Waypoint in ipairs(Waypoints) do
					local DebugPart = Instance.new("Part")
					DebugPart.Size = Vector3.new(0.5, 0.5, 0.5)
					DebugPart.Position = Waypoint.Position + Vector3.new(0, 2, 0)
					DebugPart.Material = Enum.Material.Neon
					DebugPart.Parent = workspace
					if Waypoint.Action == Enum.PathWaypointAction.Jump then
						Dummy.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						Dummy.Humanoid:MoveTo(Waypoint.Position)
					end
				end -- for loop
				-- outside for loop
			end
			--Dummy.Humanoid:MoveTo(ReturnedTarget.Position)
		--else
			--Dummy.Humanoid:MoveTo(Dummy.HumanoidRootPart.Position + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50)))
		end
	end
end

task.spawn(RunLoopWithoutProblems)

local Debounce = false

Dummy.HumanoidRootPart.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		
		if Debounce == false then
			print("Fired")
			Humanoid:TakeDamage(5)
			Debounce = true
			task.wait(1)
			Debounce = false
		end
	end
end)

Hello, so i’m making a script with pathfinding and magnitude but the issue i’m currently experiencing is that the pathfinding script is not working and i’m not sure why. Does anyone know the reason why this pathfinding script is not working (Thanks in advance)

I see nothing wrong apart from this line:

if Waypoint.Action == Enum.PathWaypointAction.Jump then
    Dummy.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	Dummy.Humanoid:MoveTo(Waypoint.Position)
end

Basically, your NPC will only move to the waypoint if it needs to jump. Very simple fix, just replace these with:

if Waypoint.Action == Enum.PathWaypointAction.Jump then
    Dummy.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
Dummy.Humanoid:MoveTo(Waypoint.Position)
Dummy.Humanoid.MoveToFinished:Wait() -- also wait for it to reach the waypoint!
1 Like