Why will pathfinding not work?

My pathfinding script will not work! i barely ever use pathfinding, so i have no idea how to fix it.

local collectionService = game:GetService("CollectionService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local pathfindingService = game:GetService("PathfindingService")

-- constants

local REFRESH_TIME = 0.5 -- every 2 seconds reset all the connections and check for new animals

-- modules



-- functions

while wait(REFRESH_TIME) do
for _,Animal : Model in pairs(collectionService:GetTagged("Animal")) do
	local Humanoid = Animal:WaitForChild("Humanoid") -- since the humanoid is loaded, just assume everything else is
	local primaryPart = Animal.PrimaryPart
	local AnimationController = Animal:FindFirstChildOfClass("AnimationController")
	local Animator = AnimationController:FindFirstChildOfClass("Animator")
	
	local CurrentState = Animal:GetAttribute("CurrentState")
	
	
	local partsInRadius = workspace:GetPartBoundsInRadius(primaryPart.Position,20)
	
	for _,Part in pairs(partsInRadius) do
		if Part.Parent:FindFirstChild("Humanoid") and players:GetPlayerFromCharacter(Part.Parent) then
			
			local path = pathfindingService:CreatePath({
				AgentRadius = 3,
				AgentHeight = 2
			})
			
			local success,err = pcall(function()
				path:ComputeAsync(primaryPart.Position,Part.Position)
			end)
			
			if path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()
				
				task.spawn(function()
				for _,Point in waypoints do
					print(Point)
					Humanoid:MoveTo(Point.Position)
					Humanoid.MoveToFinished:Wait()
				end
			end)
			end
		end
	end
	
	
	if CurrentState == "Idle" then
		
	end
			
end
end

the animal will not move at all, everything is un anchored, and it just stays still and prints the waypoints position.

Thanks for any help.

1 Like

It might be because you are overriding the pathfinding every time you have a new part in radius, telling the humanoid to move to multiple parts rather than just finding a singular part and focusing on that.

I have it set to wait before moving to the next part though, and even if that was the problem, it would at least move. Right now it just sits still. The problem might be with the rig

Idk try this:


local collectionService = game:GetService("CollectionService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local pathfindingService = game:GetService("PathfindingService")

-- constants

local REFRESH_TIME = 0.5 -- every 2 seconds reset all the connections and check for new animals

-- modules



-- functions

local function MoveToGoal(waypoints:{PathWaypoint},Humanoid:Humanoid):()
	for _,Point in waypoints do
		print(Point)
		Humanoid:MoveTo(Point.Position)
		Humanoid.MoveToFinished:Wait()
	end
end

while true do
	for _,Animal : Model in collectionService:GetTagged("Animal") do
		local Humanoid = Animal.Humanoid::Humanoid -- since the humanoid is loaded, just assume everything else is
		local primaryPart = Animal.PrimaryPart
		local AnimationController = Animal.AnimationController::AnimationController
		local Animator = AnimationController.Animator::Animator

		local CurrentState = Animal:GetAttribute("CurrentState")


		local partsInRadius = workspace:GetPartBoundsInRadius(primaryPart.Position,20)

		for _,Part in partsInRadius do
			if Part.Parent:FindFirstChild("Humanoid") and players:GetPlayerFromCharacter(Part.Parent) then

				local path = pathfindingService:CreatePath({
					AgentRadius = 3,
					AgentHeight = 2
				})

				pcall(path.ComputeAsync,path,primaryPart.Position,Part.Position)

				if path.Status == Enum.PathStatus.Success then
					task.spawn(MoveToGoal,path:GetWaypoints()::{PathWaypoint},Humanoid)
				end
			end
		end


		if CurrentState == "Idle" then

		end

	end
	task.wait(REFRESH_TIME)
end

still nothing, it prints a position, and does not move

Maybe you need to visualize it somehow?

local part = Instance.new("Part")
part.Anchored=true
part.CanCollide=false
part.CanQuery=false
part.CanTouch=false
part.CastShadow=false
part.Material=Enum.Material.Neon
part.Size = Vector3.one
part.Parent=workspace
print(part)

local function MoveToGoal(waypoints:{PathWaypoint},Humanoid:Humanoid):()
	for _,Point in waypoints do
		part.Position=Point.Position
		Humanoid:MoveTo(Point.Position)
		Humanoid.MoveToFinished:Wait()
	end
end

the part appears, but only directly under the animal, and not towards my character.

Please explain further.
I don’t understand.
Is it a problem with path computing or what?

i literally have no idea. the part spawns under the animal, the parts do not go towards my character. so i would assume that the waypoints are not even being made

i fixed it, for some reason i didn’t think to create motor6d’s in the model, and the humanoid never detected the root part, As an actual root part.

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