So, I’ve been practicing a bit AI Pathfinding Service and it seems to everything works perfectly fine. Even though my NPC is always following his path to the correct destination.
Since using a while true do loop and making NPC to follow the path inside of this loop, my NPC is always going in a loop. He won’t stop and turn back or anything.
Using “Tables” and math.random() to make this NPC go randomly to some defined destinations would work?
Here is the script I’ve made.
local PathfindingService = game:GetService("PathfindingService")
local cartoon = game.Workspace.Cartoon
local humanoid = cartoon.Humanoid
local destination = workspace.PinkFlag
local destination2 = workspace.GreenFlag
local destination3 = workspace.YellowFlag
while true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(cartoon.HumanoidRootPart.Position, destination.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
wait(math.random(2,10))
local path = PathfindingService:CreatePath()
path:ComputeAsync(cartoon.HumanoidRootPart.Position, destination2.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
wait(math.random(2,10))
local path = PathfindingService:CreatePath()
path:ComputeAsync(cartoon.HumanoidRootPart.Position, destination3.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
wait(math.random(2,10))
end