Making NPC move randomly using AI Pathfinding service?

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
7 Likes

I dont fully understand your question here? Are you asking us why the script doesn’t work, if the idea will work, or how to do it?

2 Likes

I would recommend using the Random Instance, which is much more random then math.random.

local rand = Random.new()
local n = rand:GetNextInteger(1,20)
1 Like

@REALTimothy0812 math.random was updated to use the same algorithm as the Random API, last I recall. The difference between which API is used here does not matter, nor is it relevant. Please stay on-topic.


Don’t understand what your problem is, OP. See GreekForge’s post - I have the same questions.

1 Like

The problem is that I want to make my NPC move randomly between destinations.

1 Like

The problem is that my NPC is always going to destination, after that he goes to destination2 and finally destination3. He is doing that in a loop. I just want to remake this script so my NPC will go randomly between these destinations.

1 Like

The problem here is that it’s not all that flexible. You got a separate variable for every separate destination, which means you’ll have to edit the code every time you add a new one.

Arrays save the day.
Simply store every destination inside of a table and choose randomly from there:

local pfs = game:GetService("PathfindingService")

local destinations = {
    workspace.PinkFlag;
    workspace.GreenFlag;
    workspace.YellowFlag;
}

while true do
    wait(math.random()*8+2)
    local destination = destinations[math.random(1,#destinations)] -- will choose a random key within the table
    local path = pfs:CreatePath()
    path:ComputeAsync(yourPosition,destination.Position)
    for _,waypoint in pairs(path:GetWaypoints()) do
        humanoid:MoveTo(waypoint.Position)
        humanoid.MoveToFinished:Wait()
    end
end

If my script is wrong, feel free to point me out. It’s 8 on the morning :stuck_out_tongue:

6 Likes

Thank you!