NPCs start stuttering after some time

I have my NPCs moving in a loop and my Goal is that they do this smoothly. Unfortunately they keep stuttering after a given amount of time. I thought that maybe the issue is the amount of times i call the loop so i lowered that but it still starts stuttering after some time.

for i, NPC in Active_NPCs do 
		print(NPC)
		
		local NPChum = NPC.Humanoid
		
		
		if NPC:GetAttribute("ChosenPath") == nil then
			NPC:SetAttribute("ChosenPath",math.random(1,#Destinations))
		end
		
		if NPC:GetAttribute("Moving") == false and NPC:GetAttribute("MogBattle") == false then
			warn("NPC CHOSEN")
			
			task.spawn(function()	
			
				if NPC:GetAttribute("DestinationReached") then
					
					for i = #Destinations[NPC:GetAttribute("ChosenPath")], 1, -1 do
						
						local Waypoint = Destinations[NPC:GetAttribute("ChosenPath")][i]
						NPC:SetAttribute("Moving",true)
						NPChum:MoveTo(Waypoint.Position)
						NPChum.MoveToFinished:Wait()
						
						
						if i == 1 then 
							NPC:SetAttribute("DestinationReached",false)
							NPC:SetAttribute("Moving",false)
							NPC:SetAttribute("ChosenPath",math.random(1,#Destinations))
							task.delay(.1,MoveNPC)
							
						end
					end
					
					
					
				else
					
					for i, Waypoint in ipairs(Destinations[NPC:GetAttribute("ChosenPath")]) do
						NPC:SetAttribute("Moving",true)
						print("Im moving")
						local NPChum = NPC.Humanoid
						NPChum:MoveTo(Waypoint.Position)
						NPChum.MoveToFinished:Wait()
						
						if i == #Destinations[NPC:GetAttribute("ChosenPath")] then
							NPC:SetAttribute("DestinationReached",true)
							NPC:SetAttribute("Moving",false)
							task.delay(.1,MoveNPC)
						end
					end
					
				end
				
			end)
				
		end
		
	end
	
end

I don’t know if it will help, bit can you try setting humanoidrootpart’s network owner to nil?

I wasnt expecting your solution to work but it did! Can you elaborate on why this even fixes my issue?
Thanks a LOT!!!

Its pretty hard to explain so you should just watch this video:
Watch from 5:07 to 8:33

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