I am doing pathfinding for my NPCs based on a table of destinations that can vary in the amount of destinations.
It doesn’t throw any error but the NPC does not walk to any of the destinations.
local function GetPath(NPC, destination) --Creates the path for the guard to follow
local path = Pathfinding:CreatePath()
if typeof(destination) == Vector3 then
path:ComputeAsync(NPC.HumanoidRootPart.Position,destination)
else
path:ComputeAsync(NPC.HumanoidRootPart.Position,destination.Position)
end
return path
end
local function WalkToWaypoints(NPC, tableWaypoints) --Walks to the waypoints
for i,v in pairs(tableWaypoints) do
NPC.Humanoid:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
NPC.Humanoid.Jump = true
end
NPC.Humanoid.MoveToFinished:Wait()
end
end
local function WalkTo(NPC, destination, WTable) --All of the above
local path = GetPath(NPC, destination.Position)
if path.Status == Enum.PathStatus.Success then
WalkToWaypoints(path:GetWaypoints())
task.wait(5)
WalkTo(NPC,WTable[math.random(1,#WTable)],WTable)
else
WalkTo(NPC,WTable[math.random(1,#WTable)],WTable)
end
end
--//Setup Function\\--
local function SetupAI(NPC) --Setups the AI and the function loop
local NeckPosition = NPC.Torso.Neck.C0 --Saves the position of the neck
CollectionService:AddTag(NPC, NPC.Humanoid.GuardType.Value)
WalkTo(NPC,BankWaypoints[math.random(1,#BankWaypoints)],BankWaypoints)
RunService.Heartbeat:Connect(function()
NPCFov(Character,NPC,NeckPosition)
end)
end