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
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.
--// ... \\--
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.
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