Humanoid Not moving towards part if too far


So i have noticed this.
This NPC wont walk towards the part like expected.
But the other ones do.

I tried many solutions but none worked. But when i remove the pathfinding it does walk towards the part. Any help?

local NPC = script.Parent
local ha = script.Parent:FindFirstChildWhichIsA("Humanoid")
local pathfinding_service = game:GetService("PathfindingService")
local debounce = false
local tabl = {game.Workspace.P1,game.Workspace.P2,game.Workspace.p3}
local Pathrough =  tabl[math.random(1, #tabl)] 
local Prompts = {
	[1] = ("Moving.");
	[2] = ("Heading To Location.");
	[3] = ("Moving To Location");
};
coroutine.wrap(function()
	while wait(2)  do
		game:GetService("Chat"):Chat(script.Parent.HumanoidRootPart, Prompts[math.random(1,#Prompts)], Enum.ChatColor.Red)
	end

end)()
function getClosestPlayer()
	local closest_player, closest_distance = nil, 1000
	for i, player in pairs(workspace:GetChildren()) do
		if Pathrough then
			local distance = (NPC.PrimaryPart.Position - Pathrough.Position).Magnitude
			if distance < closest_distance then
				closest_player = Pathrough
				closest_distance = distance
			end
		end
	end
	return closest_player, closest_distance
end

while true do
	local path = pathfinding_service:CreatePath(({
		["AgentRadius"] = 100000000000,
		["AgentHeight"] = 0,
		["AgentCanJump"] = false,
	}))

	local player, distance = getClosestPlayer()
	if player and distance > 1 then
		path:ComputeAsync(NPC.HumanoidRootPart.Position, Pathrough.Position, 10000000000)

		local waypoints = path:GetWaypoints()
		for _, waypoint in pairs(waypoints) do  
			ha:MoveTo(waypoint.Position)
			script.Parent.Monsta.MoveToFinished:Connect(function()
				script.Disabled = true
				wait(1)
				script.Disabled =false				
			end)
			while true do
				local distance = (NPC.PrimaryPart.Position - waypoint.Position).Magnitude
				local distance_from_player = (NPC.PrimaryPart.Position -Pathrough.Position).Magnitude
				if distance < 34999 then
					break
				end
				if distance_from_player < 109999 then
					ha:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
					break
				end
				wait()
			end

			for i, waypoint in pairs(waypoints) do
				if waypoint.Action == Enum.PathWaypointAction.Jump  then
					
				end
			end
		end
	end
	wait()
end

Lol nvm found a solution i just had to remove the useless lines :sweat_smile:

Sorry for the reply out of the blue, however could you mark this as Solved?

Ok Its done so if anyone Humanoid isnt moving this might work. :grin:

1 Like