How can I make NPC wander for a certain amount of time?

  1. What do you want to achieve?
    After the NPC completes this:
while true do
	wait (5)
char = script.Parent
pathFinder = game:GetService("PathfindingService")

path = pathFinder:CreatePath()

path:ComputeAsync(char.HumanoidRootPart.Position,workspace.guipart.Position)

for i, wayPoint in pairs(path:GetWaypoints()) do
	char.Humanoid:MoveTo(wayPoint.Position)

	if wayPoint.Action == Enum.PathWaypointAction.Jump then
		char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end

	char.Humanoid.MoveToFinished:wait()	
end

wait(20) ---how much time inbetween movements

char = script.Parent
pathFinder = game:GetService("PathfindingService")

path = pathFinder:CreatePath()

path:ComputeAsync(char.HumanoidRootPart.Position, workspace.Dest.Position)

for i, wayPoint in pairs(path:GetWaypoints()) do
	char.Humanoid:MoveTo(wayPoint.Position)

	if wayPoint.Action == Enum.PathWaypointAction.Jump then
		char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
	char.Humanoid.MoveToFinished:wait()	

end
	wait (40)
	end

I’d like the NPC to wander for a certain amount of time before repeating the script.

  1. What is the issue?
    Not sure how to achieve what I’d like to and was hoping the knowledgeable individuals on the forum could help!

  2. What solutions have you tried so far?
    I’ve tried adding a wandering script from a Wandering NPC model into the script I’ve written but it didn’t work :confused: Not a lot of information on the DevForum either :frowning:

Thank you!

I tend to create a folder with all the possible positions NPCs can wander to, then chose a random location from the folder to move to each time. So using this as the basis for your script, it becomes:

local char = script.Parent
char.PrimaryPart:SetNetworkOwner(nil)	-- Stop the jerky movement we see when clode to our player
local pathFinder = game:GetService("PathfindingService")
local destinations = workspace.Destinations:GetChildren()	-- A folder with all possible destination parts in

-- MAIN LOOP
while true do
	task.wait (5)
	local newDestination = destinations[math.random(1, #destinations)] -- choose a new destination each time
	
	local path = pathFinder:CreatePath()
	path:ComputeAsync(char.HumanoidRootPart.Position, newDestination.Position)

	for i, wayPoint in pairs(path:GetWaypoints()) do
		char.Humanoid:MoveTo(wayPoint.Position)
		if wayPoint.Action == Enum.PathWaypointAction.Jump then
			char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		char.Humanoid.MoveToFinished:Wait()	
	end
end

maybe this video might help