Complete beginner with pathfinding here. I’ve created a system that spawns in rigs, and am attempting to make a function that pathfinds the NPCs to a particular point. As it stands, the function I wrote is returning an error of “Nil”. I am not sure what I am doing wrong. Any advice is appreciated!
local PathfindingService = game:GetService("PathfindingService")
local Rig = game.ServerStorage.Rig
local SpawnPart = game.Workspace.SpawnPart
local Words = require(game.ServerScriptService.Words)
local function InitializePath (EnemyNPC)
local humanoid = EnemyNPC:WaitForChild("Humanoid")
local torso = EnemyNPC:WaitForChild("Torso")
local path = PathfindingService:CreatePath()
local success, errorMessage = pcall(function()
path:ComputeAsync(torso.Position, Vector3.new(torso.Position.X - 136, torso.Position.Y, torso.Position.Z))
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
print(errorMessage)
end
end
-- function that spawns the "enemies"
local function SpawnEnemy(Word)
local Enemy = Rig:Clone()
local humanoid = Enemy:WaitForChild("Humanoid")
local torso = Enemy:WaitForChild("Torso")
local ZCoordinate = math.random((SpawnPart.Position.Z - SpawnPart.Size.X/2), (SpawnPart.Position.Z + SpawnPart.Size.X/2))
Enemy.Name = ""
Enemy.Parent = game.Workspace
Enemy:MoveTo(Vector3.new(SpawnPart.Position.X, SpawnPart.Position.Y + 10, ZCoordinate))
InitializePath(Enemy)
end
SpawnEnemy()
Any ideas of how I could fix it? The NPCs spawn along the transparent green line seen on the bottom. Their goal is their X position, minus 134 studs, which is meant to bring them right up against the grey wall. The closest black part, which has collisions enabled, has a pathfinding modifier with passthrough set to true, while the others are cancollide false.