Npc's randomly changing path

so i was making some walking npc’s and noticed that they just randomly stops following the parts, im using MoveTo

while wait() do

	script.Parent:MoveTo(game.Workspace.TestNpc.Part1.Position)
	script.Parent.MoveToFinished:Wait()
	script.Parent:MoveTo(game.Workspace.TestNpc.Part2.Position)
	script.Parent.MoveToFinished:Wait()
	script.Parent:MoveTo(game.Workspace.TestNpc.Part3.Position)
	script.Parent.MoveToFinished:Wait()
	script.Parent:MoveTo(game.Workspace.TestNpc.Part4.Position)
	script.Parent.MoveToFinished:Wait()
	script.Parent:MoveTo(game.Workspace.TestNpc.Part5.Position)
	script.Parent.MoveToFinished:Wait()
end

image

and if someone knows how to disable the collisions, i turned off the cancollide but they still have collisions

use collision groups, as for your “changing path” issue, idk, just use pathfinding service or something!!!

1 Like

Collision groups worked thx, now ill try to use pathfinding and let you know.

i assume you want your npcs to walk from one side of the map to the other

there are 2 methods;


  1. just use moveto lol
if npc is on part1 then move to part2

elseif npc is on part2 then move to part1

  1. pathfind
if npc is on part1 then create a path to part2 and walk along it

elseif npc is on part2 then create a path to part1 and walk along it

yes this is pseudo code

Humanoid:MoveTo() has a 8 second limit

Use this code if you want to disable the limit:

local function moveTo(humanoid, targetPoint, andThen)
	local targetReached = false

	-- listen for the humanoid reaching its target
	local connection
	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
		if andThen then
			andThen(reached)
		end
	end)

	-- start walking
	humanoid:MoveTo(targetPoint)

	-- execute on a new thread so as to not yield function
	task.spawn(function()
		while not targetReached do
			-- does the humanoid still exist?
			if not (humanoid and humanoid.Parent) then
				break
			end
			-- has the target changed?
			if humanoid.WalkToPoint ~= targetPoint then
				break
			end
			-- refresh the timeout
			humanoid:MoveTo(targetPoint)
			task.wait(6)
		end

		-- disconnect the connection if it is still connected
		if connection then
			connection:Disconnect()
			connection = nil
		end
	end)
end

local function andThen(reached)
	print((reached and "Destination reached!") or "Failed to reach destination!")
end

moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)

Source

yeah no dw, i was gonna use pathfinding any way since i will have decoration in the future that may become an obstacle for the npc’s

1 Like

oooh, didn’t know this one, thx

ok yeah , used pathfinding and now it works, thx for the help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.