NPC getting stuck in place while using Pathfinding

Alright so I’m currently in the process of making an AI that will move to a random part in a dedicated folder. The issue is it currently teleports to the starting place like normal but then stays there while occasionally playing the walking animation. Its my first time messing with pathfinding so I might just be missing something but Idk.

Heres the code for the function that handles teleporting the AI to the random starting place

-- Active logic
local function activeTeleport()
    local targetPart = getRandomActivePart()
    if targetPart then
        BigFella:PivotTo(targetPart.CFrame)
    end
    ModeValue.Value = MODES.PassiveWander -- Changes the current state from Active to PW after it teleports
end

Here’s the snippet that handles the movement.

local function passiveWander()
	local targetPart = getRandomPassivePart()
	if not targetPart then
		playIdle()
		task.wait()
		return
	end

	local path = PathfindingService:CreatePath({
		AgentRadius = 8, -- Actual Radius is 18
		AgentHeight = 24,
		AgentCanJump = true, -- Might need to disable that later
		AgentJumpHeight = 10,
		AgentMaxSlope = 45
	})
	path:ComputeAsync(HumanoidRootPart.Position, targetPart.Position)

	if path.Status ~= Enum.PathStatus.Success then
		playIdle()
		task.wait()
		return
	end

	local waypoints = path:GetWaypoints()
	playWalk()

	for _, waypoint in ipairs(waypoints) do
		if ModeValue.Value ~= MODES.PassiveWander then break end -- Makes sure the current state is set to PW
		Humanoid:MoveTo(waypoint.Position)
		local reached = false
		local conn
		conn = Humanoid.MoveToFinished:Connect(function()
			reached = true
			if conn then conn:Disconnect() end
		end)

		local timer = 0
		while not reached and ModeValue.Value == MODES.PassiveWander do
			-- Check for nearby players to switch current state to Aggro
			for _, player in pairs(Players:GetPlayers()) do
				local char = player.Character
				if char and char:FindFirstChild("HumanoidRootPart") then
					if (char.HumanoidRootPart.Position - HumanoidRootPart.Position).Magnitude <= AGGRO_DISTANCE then
						ModeValue.Value = MODES.Aggro
						stopAllAnimations()
						return
					end
				end
			end
			task.wait(0.2)
			timer = timer + 0.2
			if timer > 10 then break end
		end
	end

	playIdle()
	task.wait(math.random(.1,.5)) -- SHOULD let it wait before moving on :/
end

and if it helps here is the random part function

local function getRandomPassivePart()
	local parts = {}
	for _, obj in Workspace.LamePaths:GetChildren() do
		if obj:IsA("BasePart") then
			table.insert(parts, obj)
		end
	end
	if #parts > 0 then
		return parts[math.random(1, #parts)]
	end
	return nil
end

I was originally using

Humanoid:MoveTo(targetPart.Position)

instead of pathfinding services but I was tired of the AI getting stuck on things.

1 Like

What stands out to me the most is in here.

local path = PathfindingService:CreatePath({
		AgentRadius = 8, -- Actual Radius is 18
		AgentHeight = 24,
		AgentCanJump = true, -- Might need to disable that later
		AgentJumpHeight = 10,
		AgentMaxSlope = 45
	})

First of all, your AgentRadius and AgentHeight are quite big, though I do not think this is the problem.

I think that your problem lies in the agent parameters. You have included “AgentJumpHeight” and “AgentMaxSlope” in there, but this is not right as they are properties of a humanoid.

There is also a funny possibility that you have forgotten to unanchor some part.

2 Likes
local path = PathfindingService:CreatePath({
    AgentRadius = 8,
    AgentHeight = 24,
    AgentCanJump = true,
    AgentJumpHeight = 10,
    AgentMaxSlope = 45,
    AgentMaterialCosts = {
        [Enum.Material.Concrete] = 100
    }
})

Now the NPC will avoid Concrete. From here, it just takes some clever placement of the Concrete or whatever material you choose to use as your blocker. You could even make a new material that matches the one that is there, but is actually set up as a blocker.

Thats good to know

Yeah that was it…

1 Like

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