Pathfinding doesn't generate waypoints

Hello, I need some help figuring out why my AI is confused. My script looks for the nearest player and if there is a nearest player the AI will chase them, if not my AI gets confused. Out of nowhere, my AI randomly broke in-game and in studio, I have done some debugging and noticed that there are no waypoints. AI’s behavior:
robloxapp-20240514-1350543.wmv

I have been trying to find the solution to this, but nothing works. :frowning:
Here is my script:

while humanoid.Health > 0 and workspace:FindFirstChild("TestingPlace") do
	task.wait(math.min((60-workspace:GetRealPhysicsFPS())/59,5))
	local success, errorMsg = pcall(function()
		local players = {}
			for _, char in pairs(workspace:GetChildren()) do
				if char:FindFirstChild("Humanoid") and char ~= bot then
					table.insert(players,char)
				end
			end
			local target = nil
			for _,char in pairs(players) do
				local mag1 = (botRoot.Position - char.HumanoidRootPart.Position).Magnitude
				local mag2 = (botRoot.Position - target.HumanoidRootPart.Position).Magnitude
				if typeof(target) == "nil" then
					target = char
				elseif mag1 <= mag2 then
					target = char
				end
			end
		end
		if target then
			local path = game:GetService("PathfindingService"):CreatePath()
			path:ComputeAsync(botRoot.Position,target.HumanoidRootPart.Position)
			local waypoints = path:GetWaypoints()
			if path.Status==Enum.PathStatus.Success then
				local specificWaypoint = waypoints[math.clamp(3,1,#waypoints)]
				if specificWaypoint then
					local waitTime = (specificWaypoint.Position-botRoot.Position).Magnitude/(bot.Humanoid.WalkSpeed/.3)
					bot.Humanoid.MoveTo(specificWaypoint.Position)
					if specificWaypoint.Action == Enum.PathWaypointAction.Jump then
						bot.Humanoid.Jump = true
					end
					task.wait(waitTime)
				end
			else
				local position=botRoot.Position
				local randomVector = Vector3.new(math.random(position.X - 5,position.X + 5),position.Y,math.random(position.Z - 5,position.Z + 5))
				bot.Humanoid:MoveTo(randomVector)
				task.wait(1)
			end
		else
			local position=botRoot.Position
			local randomVector = Vector3.new(math.random(position.X - 5,position.X + 5),position.Y,math.random(position.Z - 5,position.Z + 5))
			bot.Humanoid:MoveTo(randomVector)
			task.wait(1)
		end
	end)
	if errorMsg then
		warn(string.format("bot ai (%s): %s",bot.Name,errorMsg))
	end
end

(Sorry for messy code)
I really need help with this, I don’t know how my AI broke, navmesh is fine though.

1 Like

Check if the target variable is updated within the loop over players to make sure it’s assigned the nearest player.
You probably need to focus on the section where you iterate over the players to find the nearest one and update the target variable

This is the block i reffer

for _,char in pairs(players) do
    local mag1 = (botRoot.Position - char.HumanoidRootPart.Position).Magnitude
    local mag2 = (botRoot.Position - target.HumanoidRootPart.Position).Magnitude
    if typeof(target) == "nil" then
        target = char
    elseif mag1 <= mag2 then
        target = char
    end
end

Also, correct the condition from typeof(target) == "nil" to target == nil so it would look like this

local target = nil

for _, char in pairs(players) do
    local mag1 = (botRoot.Position - char.HumanoidRootPart.Position).Magnitude
    if target == nil then  -- corrected
        target = char
    else
        local mag2 = (botRoot.Position - target.HumanoidRootPart.Position).Magnitude -- mag2 inside the condition instead in the beggining
        if mag1 < mag2 then
            target = char
        end
    end
end

Let me know how it goes :slight_smile:

1 Like

AI is still confused :frowning:

This script has been like this for a couple months now, so yeah thank you for correcting typeof(target) == "nil" to target == nil

Is the next waypoint more than 8 seconds away? From what I understand they stop after the item travelling to the waypoint will pause or stop completely after 8 seconds.

whaat? Never happened with me, I coded it so that it waits a short amount of time depending on the magnitude and walkspeed because humanoid.MoveToFinished:Wait() is delayed