Hi all! In my project, I am using the pathfinding service in order to make a NPC of mine move - or “patrol” as it is called in the script. I can’t help but think that the current system I have in place is quite boring and ugly looking for the pathfinding of this NPC, as it is setup to pathfind to random positions within a set range.
Here is the function down below:
function AIClass:Patrol()
local path = PFS:CreatePath()
local randX = math.random(self.SpawnPos.X - self.PatrollingRange, self.SpawnPos.X + self.PatrollingRange)
local randZ = math.random(self.SpawnPos.Z - self.PatrollingRange, self.SpawnPos.Z + self.PatrollingRange)
local randomPos = Vector3.new(randX, self.PrimaryPart.Position.Y, randZ)
path:ComputeAsync(self.PrimaryPart.Position, randomPos)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
self.PrimaryPart:SetNetworkOwner(nil)
for _, waypoint in pairs(waypoints) do
self.Humanoid:MoveTo(waypoint.Position)
self.Humanoid.MoveToFinished:Wait()
end
else
warn("Unsuccessful path found")
end
end
And here is where the function is called:
while true do
enemyAI:Patrol()
wait(math.random(enemyAI.MinInspectionTime, enemyAI.MaxInspectionTime))
end
If anyone has any improvements to make my “AI” more intelligent looking, feel free to send them down below - even if it is methods you have used in your past experiences with the pathfinding service!