When npc catches the player npc movement code should be start over

My movement script works like that:

go to part1.position if you arrived the position break the loop and go part2.position loop and do that for 18 times

So the issue is if npc catches the player in part4 while loop (or part10 it doesnt matter)
Npc thinks or code “I need to go part4 because im in part4 while loop” but i dont want that, i want if npc catches the player while loops start over. (i hope i explained well)

Here is my npc movement code, i did that for 18 times i dont know if its a effect to performance

while wait(1.5) do
			local distance = (NPC.HumanoidRootPart.Position - part1.Position).magnitude
			if distance <= mag then
				break
			end				
			NPC.Humanoid:MoveTo(part1.Position)	
		end

Here is the event when npc catches the player

wait()
local dum = game.Workspace.CellatBaba
local players = game:GetService("Players")
local lead = game.Players:WaitForChild("leaderstats", 1)
local Remote = game.ReplicatedStorage.CheckPoint
local t = game.Workspace.SpawnBase.TeleportPad

game.Workspace.CellatBaba.Hitbox.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		Remote:FireServer()
		game.Workspace.CellatBaba:SetPrimaryPartCFrame(CFrame.new(t.Position))
		hit.Parent.Humanoid.Health = 0
	end 	    
end)	

Why nobody replying this topic i really dont know how to do that

so you wanna to make the npc stop when he see a player?

Or kill him?

No, when npc catches the player or reach the player they will going back to start i did that but the problem is npc still going where he is at before catch

If npc going to part8 still going part8 even he catches the player

you can just make your script like this its a lot easier:

local Part0 = game.Workspace.Part0
local Part1 = game.Workspace.Part1
local Part2 = game.Workspace.Part2
local Part3 = game.Workspace.Part3
local Part = Instance.new("Part")
Part.Size = Vector3.new(16, 12, 16)
Part.Parent = script.Parent
Part.CanCollide = false
Part.Transparency = 1
Part.Massless = true
local weld = Instance.new("Weld")
weld.Parent = Part
weld.Part0 = Part
weld.Part1 = script.Parent.HumanoidRootPart
Part.Touched:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
	script.Parent.Humanoid:MoveTo(Part0.Position)
	end
end)
function touch(hit)
	if hit == game.Workspace.Part0 then
		script.Parent.Humanoid:MoveTo(Part1.Position)
	elseif hit == game.Workspace.Part1 then
		script.Parent.Humanoid:MoveTo(Part2.Position)
	elseif hit == game.Workspace.Part2 then
		script.Parent.Humanoid:MoveTo(Part3.Position)
	elseif hit == game.Workspace.Part3 then
		script.Parent.Humanoid:MoveTo(Part0.Position)
	end
end

script.Parent.RightLowerLeg.Touched:Connect(touch)
script.Parent.LeftLowerLeg.Touched:Connect(touch)
script.Parent.Humanoid:MoveTo(Part0.Position)

this need to be inside your npc model put it in a server script
so when you come close to the npc he will back to the part 0 from where he begin
you can edit the script and add other parts if you want
If you didn’t know how to use this i will give you the place

I dont think i understand but i get it little bit, if you give me place it would be great

btw npc should be client side so maybe it wont work because you said put in a server script

The localscripts cant move the npc only server script can move him

Well my npc works fine

My npc clientside i cloned in replicated storage maybe because of that i can move in localscript

allright i will do that but thats not an issue rn

check out this place maybe can help you:

Baseplate.rbxl (51.2 KB)

It kinda helped i wrote some code here it is

	local Part0 = game.Workspace.Part0
	local Part1 = game.Workspace.Part1
	local Part2 = game.Workspace.Part2
	local Part3 = game.Workspace.Part3
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(16, 12, 16)
	Part.Parent = script.Parent
	Part.CanCollide = false
	Part.Transparency = 1
	Part.Massless = true
	local weld = Instance.new("Weld")
	weld.Parent = Part
	weld.Part0 = Part
	weld.Part1 = script.Parent.HumanoidRootPart
local NPC = game.Workspace.NarakuLite.HumanoidRootPart
local mag = 40

	Part.Touched:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		script.Parent.Humanoid:MoveTo(Part0.Position)
		end
	end)
	function touch(hit)

	if hit == game.Workspace.Part0 then
		while wait(1) do
			script.Parent.Humanoid:MoveTo(Part1.Position)
			local distance = (NPC.Position - Part1.Position).magnitude
			if distance == mag  then
				break
			end		
		end			
	elseif hit == game.Workspace.Part1 then
		while wait() do
			script.Parent.Humanoid:MoveTo(Part2.Position)
		end
			
		elseif hit == game.Workspace.Part2 then
			script.Parent.Humanoid:MoveTo(Part3.Position)
		elseif hit == game.Workspace.Part3 then
			script.Parent.Humanoid:MoveTo(Part0.Position)
		end
	end


	script.Parent.RightLowerLeg.Touched:Connect(touch)
	script.Parent.LeftLowerLeg.Touched:Connect(touch)
	script.Parent.Humanoid:MoveTo(Part0.Position)

but the issue is the timeout, if you dont know humanoid.MoveTo has timeout, and this timeout is 8 second, to bypass timeout i while looped moveto scripts, the while loop broked the hit function. If is another way to bypass timeout letme know.

Maybe we can use repeat until, i will try

Still dont work. Im gonna research other solutions.

Maybe should i change moveto to pathfinding i dunno

if you wanna to bypass humanoid:MoveTo timeout you can just do like this:

local Part0 = game.Workspace.Part0
local Part1 = game.Workspace.Part1
local Part2 = game.Workspace.Part2
local Part3 = game.Workspace.Part3
local Part = Instance.new("Part")
local Repeating = "Part0"
local TouchFilter = false
local LeftTouch
local RightTouch
Part.Size = Vector3.new(16, 12, 16)
Part.Parent = script.Parent
Part.CanCollide = false
Part.Transparency = 1
Part.Massless = true
local weld = Instance.new("Weld")
weld.Parent = Part
weld.Part0 = Part
weld.Part1 = script.Parent.HumanoidRootPart
Part.Touched:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		if not TouchFilter then
			TouchFilter = true
			Repeating = " "
			repeat
				script.Parent.Humanoid:MoveTo(Part0.Position)
				wait(0.1)
			until Repeating == "Part0"
			print(Repeating)
			LeftTouch:Disconnect()
			RightTouch:Disconnect()
			TouchFilter = false
		end
	end
end)
function touch(hit)
	wait(0.2)
	if hit == game.Workspace.Part0 then
		Repeating = "Part0"
		repeat
			script.Parent.Humanoid:MoveTo(Part1.Position)
			wait(0.1)
		until Repeating == "Part1" or Repeating == " "
	elseif hit == game.Workspace.Part1 then
		Repeating = "Part1"
		repeat
			script.Parent.Humanoid:MoveTo(Part2.Position)
			wait(0.1)
		until Repeating == "Part2" or Repeating == " "
	elseif hit == game.Workspace.Part2 then
		Repeating = "Part2"
		repeat
			script.Parent.Humanoid:MoveTo(Part3.Position)
			wait(0.1)
		until Repeating == "Part3" or Repeating == " "
	elseif hit == game.Workspace.Part3 then
		Repeating = "Part3"
		repeat
			script.Parent.Humanoid:MoveTo(Part0.Position)
			wait(0.1)
		until Repeating == "Part0" or Repeating == " "
	end
end

RightTouch = script.Parent.RightLowerLeg.Touched:Connect(touch)
LeftTouch = script.Parent.LeftLowerLeg.Touched:Connect(touch)
script.Parent.Humanoid:MoveTo(Part0.Position)

that will make a loop on Humanoid:MoveTo and the loop will break when the NPC catch a player or when the player touch any of your parts, if he touch a part he will break the old loop and make another one to the new part

also you can remove Left and Right touch Disconnect() if you dont need to stop the NPC when he catch player and go back to part0

Yeah it seems like working but i need to teleport there but when i tried to teleport it stucks in air i tried CFrame i tried Vector3 both of these didnt work.

Oh sorry my bad im trying to teleport in Repet loop