Help with pathfinding ai

Hello, I’m trying to make a pathfinding ai script, but I keep getting an error.

--// ... \\--
local character = script.Parent
local hrp = character.HumanoidRootPart
local humanoid = character.Humanoid

--// Services \\--
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")


--// Functions \\--

local function GetNearestPlayer(position)
	local nearestplayer = nil
	local nearestdistance = math.huge
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character then
			if v.Character:FindFirstChild("HumanoidRootPart") then
				local position = (v.Character.HumanoidRootPart.Position - position).Magnitude
				if position < nearestdistance then
					nearestplayer = v
				end
			end
		end
	end
	if nearestplayer == nil then return end
	return nearestplayer.Character.HumanoidRootPart
end

function GenerateRandomDestination()
	local hrp = character.PrimaryPart or character:FindFirstChild("HumanoidRootPart");
	local randomPos = hrp.Position + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50));

	return randomPos;
end

function GoToNearestPlayer()
	local target = GetNearestPlayer(hrp.Position)
	local path = PathFindingService:CreatePath()
	path:ComputeAsync(target.Position, hrp.Position)
	
	for _,waypoint in pairs(path:GetWaypoints()) do
		humanoid:MoveTo(waypoint.Position)
		
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end
		humanoid.MoveToFinished:Wait()
	end
end

RunService.Heartbeat:Connect(function()
	GoToNearestPlayer()
end)

The error:

Argument 2 missing or nil  -  Server - AI:43
17:18:46.311  Stack Begin  -  Studio
17:18:46.311  Script 'Workspace.Shop.AI', Line 43 - function GoToNearestPlayer  -  Studio - AI:43
17:18:46.311  Script 'Workspace.Shop.AI', Line 56  -  Studio - AI:56
17:18:46.311  Stack End  -  Studio
1 Like

This might help since it is an api doc Humanoid | Roblox Creator Documentation

I’m not asking for docs, I know what Humanoid:MoveTo does. I’m asking for help with the error.

yes and the error is specifically saying that a second parameter is missing, although I am confused on what parameter that happens to be since I haven’t had to add more than one parameter at a time.

I changed some stuff up:

--// ... \\--
local character = script.Parent
local hrp = character.HumanoidRootPart
local humanoid = character.Humanoid

--// Services \\--
local PathFindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")


--// Functions \\--
function GetNearestPlayer()
	local test = game.Workspace:GetChildren()

	local max_dist = 50
	local target = nil
	local test_hrp
	for _,v in pairs(test) do
		if v:IsA("Model") and game.Players:FindFirstChild(v.Name) then

			test_hrp = v:FindFirstChild("HumanoidRootPart")

			if (hrp.Position - test_hrp.Position).magnitude < max_dist then
				target = test_hrp
			end
		end
	end
	if target == nil then return end
	return target
end

function GenerateRandomDestination()
	local hrp = character.PrimaryPart or character:FindFirstChild("HumanoidRootPart");
	local randomPos = hrp.Position + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50));

	return randomPos;
end

function GoToNearestPlayer()
	local target = GetNearestPlayer()
	local path = PathFindingService:CreatePath()
	path:ComputeAsync(target.Position, hrp.Position)
	
	for _,waypoint in pairs(path:GetWaypoints()) do
		humanoid:MoveTo(waypoint.Position)
		
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end
		humanoid.MoveToFinished:Wait()
	end
end

--RunService.Heartbeat:Connect(function()
	GoToNearestPlayer()
--end)

Error:

Workspace.Shop.AI:45: attempt to index nil with 'Position'  -  Server - AI:45
  19:05:39.585  Stack Begin  -  Studio
  19:05:39.585  Script 'Workspace.Shop.AI', Line 45 - function GoToNearestPlayer  -  Studio - AI:45
  19:05:39.585  Script 'Workspace.Shop.AI', Line 58  -  Studio - AI:58
  19:05:39.585  Stack End  -  Studio

I’m fairly sure the issue here is that sometimes the AI isn’t able to generate a path which means that an error will always occur. I suggest adding a check to see if the AI is blocked; or if the path failed to generate.

How could I do that?
characters

Its always best to put anything that has a chance of failing into a pcall

I’m pretty sure the error is coming from the GetNearestPlayer function. “target” is nil. “attempt to index nil with ‘Position’”. I need to figure out how to make target not nil.

function GetNearestPlayer()
	local test = game.Workspace:GetChildren()

	local max_dist = 50
	local target = nil
	local test_hrp
	for _,v in pairs(test) do
		if v:IsA("Model") and game.Players:FindFirstChild(v.Name) then

			test_hrp = v:FindFirstChild("HumanoidRootPart")

			if (hrp.Position - test_hrp.Position).magnitude < max_dist then
				target = test_hrp
			end
		end
	end
	if target == nil then return end
	return target
end

In that case define the target variable outside of that function since every time it runs it gets redefined as nil.

Simply put, your not checking if the waypoint.Position is nil. At least that’s what it seems.

Well we wont know what target is until there’s a player within 50 studs.

It’s something to do with target being nil.

But if it gets redefined everytime then that could just cause problems, as I am suspecting

Then how do I prevent it from being a problem?

define it outside of the scope

I’m confused.

We won’t know what target is until there’s a player within 50 studs. Could you provide code or explain more?

local target

function a()
  target = plrname
end)

Try the above. It was edited…

Use ipairs to iterate over waypoints