My pathfinding ai doesn't work properly

oh yeah, it’s like it can’t get me because it’s “impossible” to get off the part sometimes

give me a moment i am making a version that can jump

I made it already though (charLimit)

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Bot = script.Parent
local Humanoid = Bot.Humanoid
local RootPart = Bot.HumanoidRootPart
local RecalculationInterval = math.random(0.5, 1.0)  -- Increased interval to reduce twitching
local MinimumRecalculationDistance = 5  -- Recalculate if target moves more than 5 studs
local waypoints
local NextWaypointIndex
local ReachConnection
local BlockConnection

-- Function to find the nearest player
local function findNearestPlayer()
    local nearestPlayer = nil
    local nearestDistance = math.huge

    for _, player in pairs(Players:GetPlayers()) do
        if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
            local distance = (RootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
            if distance < nearestDistance then
                nearestDistance = distance
                nearestPlayer = player
            end
        end
    end

    return nearestPlayer
end

-- Function to check if the bot is stuck on a slope
local function isStuckOnSlope()
    local rayOrigin = RootPart.Position
    local rayDirection = Vector3.new(0, -1, 0)  -- Raycast downward
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {Bot}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

    local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 10, raycastParams)
    if raycastResult then
        local normal = raycastResult.Normal
        local slopeAngle = math.deg(math.acos(normal.Y))  -- Calculate slope angle
        return slopeAngle > 20  -- Jump if the slope is steeper than 20 degrees
    end
    return false
end

local function FollowPath(targetPosition)
    -- Avoid recalculating if the bot is already close to the target
    if (RootPart.Position - targetPosition).Magnitude < MinimumRecalculationDistance then
        return
    end

    local path = PathfindingService:CreatePath({
        AgentRadius = 2,  -- Adjust based on bot size
        AgentHeight = 5,  -- Adjust based on bot size
        AgentCanJump = true,
        WaypointSpacing = 4,  -- Smaller value for more detailed waypoints
    })

    local success, errorMessage = pcall(function()
        path:ComputeAsync(RootPart.Position, targetPosition)
    end)

    if success and path.Status == Enum.PathStatus.Success then
        waypoints = path:GetWaypoints()
        NextWaypointIndex = 2

        if ReachConnection then ReachConnection:Disconnect() end
        if BlockConnection then BlockConnection:Disconnect() end

        BlockConnection = path.Blocked:Connect(function(blockedWaypointIndex)
            if blockedWaypointIndex >= NextWaypointIndex then
                BlockConnection:Disconnect()
                FollowPath(targetPosition)
            end
        end)

        ReachConnection = Humanoid.MoveToFinished:Connect(function(reached)
            if reached and NextWaypointIndex < #waypoints then
                -- Check if the next waypoint requires jumping
                if waypoints[NextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
                    print("Jumping!")
                    Humanoid.Jump = true
                end

                NextWaypointIndex += 1
                Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
            else
                ReachConnection:Disconnect()
                BlockConnection:Disconnect()
            end
        end)

        Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
    else
        warn("Pathfinding failed:", errorMessage)
    end
end

-- Main loop for staggered recalculation
while wait(RecalculationInterval) do
    local nearestPlayer = findNearestPlayer()
    if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
        FollowPath(nearestPlayer.Character.HumanoidRootPart.Position)
    end

    -- Force jump if stuck on a slope
    if isStuckOnSlope() then
        print("Stuck on slope, jumping!")
        Humanoid.Jump = true
    end
end

try this one, let me know if it worked

local PathfindingService = game:GetService(“PathfindingService”)
local Players = game:GetService(“Players”)
local Bot = script.Parent
local Humanoid = Bot.Humanoid
local MinimumRecalculationDistance = 5 – Recalculate if target moves more than 5 studs
local waypoints
local NextWaypointIndex
local ReachConnection
local BlockConnection

– Function to find the nearest player
local function findNearestPlayer()
local nearestPlayer = nil
local nearestDistance = math.huge

for _, player in pairs(Players:GetPlayers()) do
	if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
		local distance = (Bot.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
		if distance < nearestDistance then
			nearestDistance = distance
			nearestPlayer = player
		end
	end
end

return nearestPlayer

end

local function FollowPath(targetPosition)
– Avoid recalculating if the bot is already close to the target
if (Bot.HumanoidRootPart.Position - targetPosition).Magnitude < MinimumRecalculationDistance then
return
end

local path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = true,
	WaypointSpacing = math.huge,
})

local success, errorMessage = pcall(function()
	path:ComputeAsync(Bot.HumanoidRootPart.Position, targetPosition)
end)

if success and path.Status == Enum.PathStatus.Success then
	waypoints = path:GetWaypoints()
	NextWaypointIndex = 2

	if ReachConnection then ReachConnection:Disconnect() end
	if BlockConnection then BlockConnection:Disconnect() end

	BlockConnection = path.Blocked:Connect(function(blockedWaypointIndex)
		if blockedWaypointIndex >= NextWaypointIndex then
			BlockConnection:Disconnect()
			FollowPath(targetPosition)
		end
	end)

	ReachConnection = Humanoid.MoveToFinished:Connect(function(reached)
		if reached and NextWaypointIndex < #waypoints then
			NextWaypointIndex += 1
			if waypoints[NextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
				Humanoid.Jump = true
			end
			Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
		else
			ReachConnection:Disconnect()
			BlockConnection:Disconnect()
		end
	end)

	Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
end

end

– Main loop for staggered recalculation
while task.wait() do
local nearestPlayer = findNearestPlayer()
if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild(“HumanoidRootPart”) then
FollowPath(nearestPlayer.Character.HumanoidRootPart.Position)
end
end

this one has jump but sometimes it’s kinda buggy

can you show me a screenshot or a video showing how its getting stuck

yeah so your code can’t make it jump

try sending the model with the code and ill try to fix it


like this, it’s like it can’t get around me because there is no path, according to it

i recreated the scene where it got stuck and it is doing the same for me, i will try to fix it

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Bot = script.Parent
local Humanoid = Bot.Humanoid
local RootPart = Bot.HumanoidRootPart
local RecalculationInterval = math.random(0.5, 1.0)  -- Increased interval to reduce twitching
local MinimumRecalculationDistance = 5  -- Recalculate if target moves more than 5 studs
local waypoints
local NextWaypointIndex
local ReachConnection
local BlockConnection

-- Function to find the nearest player
local function findNearestPlayer()
	local nearestPlayer = nil
	local nearestDistance = math.huge

	for _, player in pairs(Players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			local distance = (RootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
			if distance < nearestDistance then
				nearestDistance = distance
				nearestPlayer = player
			end
		end
	end

	return nearestPlayer
end

-- Function to check if the bot is stuck on a slope
local function isStuckOnSlope()
	local rayOrigin = RootPart.Position
	local rayDirection = Vector3.new(0, -1, 0)  -- Raycast downward
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Bot}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 10, raycastParams)
	if raycastResult then
		local normal = raycastResult.Normal
		local slopeAngle = math.deg(math.acos(normal.Y))  -- Calculate slope angle
		return slopeAngle > 20  -- Jump if the slope is steeper than 20 degrees
	end
	return false
end

-- Function to check if the bot is facing a wall
local function isFacingWall()
	local rayOrigin = Bot.LowerTorso.Position
	local rayDirection = Bot.LowerTorso.CFrame.LookVector  -- Raycast in the direction the bot is facing
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Bot}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 5, raycastParams)  -- Check 5 studs ahead
	if raycastResult then
		return true  -- Wall detected
	end
	return false
end

local function FollowPath(targetPosition)
	-- Avoid recalculating if the bot is already close to the target
	if (RootPart.Position - targetPosition).Magnitude < MinimumRecalculationDistance then
		return
	end

	local path = PathfindingService:CreatePath({
		AgentRadius = 2,  -- Adjust based on bot size
		AgentHeight = 5,  -- Adjust based on bot size
		AgentCanJump = true,
		WaypointSpacing = 4,  -- Smaller value for more detailed waypoints
	})

	local success, errorMessage = pcall(function()
		path:ComputeAsync(RootPart.Position, targetPosition)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		NextWaypointIndex = 2

		if ReachConnection then ReachConnection:Disconnect() end
		if BlockConnection then BlockConnection:Disconnect() end

		BlockConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= NextWaypointIndex then
				BlockConnection:Disconnect()
				FollowPath(targetPosition)
			end
		end)

		ReachConnection = Humanoid.MoveToFinished:Connect(function(reached)
			if reached and NextWaypointIndex < #waypoints then
				-- Check if the next waypoint requires jumping
				if waypoints[NextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
					print("Jumping!")
					Humanoid.Jump = true
				end

				NextWaypointIndex += 1
				Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
			else
				ReachConnection:Disconnect()
				BlockConnection:Disconnect()
			end
		end)

		Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
	else
		warn("Pathfinding failed:", errorMessage)
	end
end

-- Main loop for staggered recalculation
while wait(RecalculationInterval) do
	local nearestPlayer = findNearestPlayer()
	if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
		FollowPath(nearestPlayer.Character.HumanoidRootPart.Position)
	end

	-- Force jump if stuck on a slope
	if isStuckOnSlope() then
		print("Stuck on slope, jumping!")
		Humanoid.Jump = true
	end

	-- Force jump if facing a wall
	if isFacingWall() then
		print("Facing a wall, jumping!")
		Humanoid.Jump = true
	end
end

it has to have a lower torso otherwise you will get an error, for me it worked on the same scene you said it got stuck, let me know if it worked

also I renamed LowerTorso part to RootPart

i know whats the problem, let me send you a fix to it

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Bot = script.Parent
local Humanoid = Bot.Humanoid
local RootPart = Bot.HumanoidRootPart
local RecalculationInterval = math.random(0.5, 1.0)  -- Increased interval to reduce twitching
local MinimumRecalculationDistance = 5  -- Recalculate if target moves more than 5 studs
local waypoints
local NextWaypointIndex
local ReachConnection
local BlockConnection

-- Function to find the nearest player
local function findNearestPlayer()
	local nearestPlayer = nil
	local nearestDistance = math.huge

	for _, player in pairs(Players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			local distance = (RootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
			if distance < nearestDistance then
				nearestDistance = distance
				nearestPlayer = player
			end
		end
	end

	return nearestPlayer
end

-- Function to check if the bot is stuck on a slope
local function isStuckOnSlope()
	local rayOrigin = RootPart.Position
	local rayDirection = Vector3.new(0, -1, 0)  -- Raycast downward
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Bot}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 10, raycastParams)
	if raycastResult then
		local normal = raycastResult.Normal
		local slopeAngle = math.deg(math.acos(normal.Y))  -- Calculate slope angle
		return slopeAngle > 20  -- Jump if the slope is steeper than 20 degrees
	end
	return false
end

-- Function to check if the bot is facing a wall
local function isFacingWall()
	local rayOrigin = Bot.LowerTorso.Position
	local rayDirection = Bot.LowerTorso.CFrame.LookVector  -- Raycast in the direction the bot is facing
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Bot}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 5, raycastParams)  -- Check 5 studs ahead
	if raycastResult then
		if not raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
			return true  -- Wall detected
		end
	end
	return false
end

local function FollowPath(targetPosition)
	-- Avoid recalculating if the bot is already close to the target
	if (RootPart.Position - targetPosition).Magnitude < MinimumRecalculationDistance then
		return
	end

	local path = PathfindingService:CreatePath({
		AgentRadius = 2,  -- Adjust based on bot size
		AgentHeight = 5,  -- Adjust based on bot size
		AgentCanJump = true,
		WaypointSpacing = 4,  -- Smaller value for more detailed waypoints
	})

	local success, errorMessage = pcall(function()
		path:ComputeAsync(RootPart.Position, targetPosition)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		NextWaypointIndex = 2

		if ReachConnection then ReachConnection:Disconnect() end
		if BlockConnection then BlockConnection:Disconnect() end

		BlockConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= NextWaypointIndex then
				BlockConnection:Disconnect()
				FollowPath(targetPosition)
			end
		end)

		ReachConnection = Humanoid.MoveToFinished:Connect(function(reached)
			if reached and NextWaypointIndex < #waypoints then
				-- Check if the next waypoint requires jumping
				if waypoints[NextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
					print("Jumping!")
					Humanoid.Jump = true
				end

				NextWaypointIndex += 1
				Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
			else
				ReachConnection:Disconnect()
				BlockConnection:Disconnect()
			end
		end)

		Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
	else
		warn("Pathfinding failed:", errorMessage)
	end
end

-- Main loop for staggered recalculation
while wait(RecalculationInterval) do
	local nearestPlayer = findNearestPlayer()
	if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
		FollowPath(nearestPlayer.Character.HumanoidRootPart.Position)
	end

	-- Force jump if stuck on a slope
	if isStuckOnSlope() then
		print("Stuck on slope, jumping!")
		Humanoid.Jump = true
	end

	-- Force jump if facing a wall
	if isFacingWall() then
		print("Facing a wall, jumping!")
		Humanoid.Jump = true
	end
end

I’ll try to fix it, gimme a minute