:MoveTo() lagging

Move to is lagging. Basically what happens is that, when the player keeps moving forward, the NPC moves to its position, but before it reaches it, the player is already further than the NPC, therefore making it laggy, because the npc stops, waiting until its told to :MoveTo() again, and the script doesnt just happen in a nanosecond.

for i,v in game.Players:GetPlayers() do
		print("PlayerFound")
		
		if v.Character.Parent == game.Workspace.Characters then
			print(v,v.Character,v.Character.Parent)
			local PlayerPosition = v.Character.PrimaryPart.Position
			local RakePosition = Rake.PrimaryPart.Position
			if (RakePosition - PlayerPosition).Magnitude < 50 then
				local raycastparams = RaycastParams.new()
				raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
				raycastparams.FilterDescendantsInstances = {Rake}
				local direction = PlayerPosition - RakePosition
				local raycast = workspace:Raycast(RakePosition,direction,raycastparams)
				
				print(RakePosition,PlayerPosition)

				print("Raycasted")
				print(raycast.Instance,raycast.Instance.Parent.Parent,raycast.Instance.Parent)

				if raycast.Instance.Parent == v.Character then
					print("Found")
					hum.WalkSpeed = RunSpeed
					while true do
						task.wait()
						hum.MoveToFinished:Connect(function(reached)
							bool = true
						end)
						local function Normal()
							if bool then
								local raycastparams = RaycastParams.new()
								raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
								raycastparams.FilterDescendantsInstances = {Rake}
								local raycast = workspace:Raycast(Rake.PrimaryPart.Position,v.Character.PrimaryPart.Position,raycastparams)
								TargetPosition = v.Character.PrimaryPart.Position
								if raycast.Instance.Parent == v then
									hum:MoveTo(v.Character.PrimaryPart.Position)
									print("Moving to")
								else
									followPath(v.Character.PrimaryPart.Position)
									print("Following path")
								end
							end
						end

						if (Rake.PrimaryPart.Position - v.Character.PrimaryPart.Position).Magnitude > 50 then
							print("Too far away")
							task.wait(5)
							if (Rake.PrimaryPart.Position - v.Character.PrimaryPart.Position).Magnitude > 50 then
								print("Ended")
								hum.WalkSpeed = NormalSpeed
								break
							else
								Normal()
							end
						else
							Normal()
						end
					end
				end
			end
		end

Nevermind, thats not the only problem. The NPC does not reach its top speed either (I set the walkspeed to 100 to test it out, and the NPC was walking at less than 30. It was also laggy despite the player being far away)

In roblox when a player gets close to a part(or a npc) the player takes OWNERSHIP OVER THE PART, but when the player is faraway from the part the server takes ownership of it again which can cause lag when switching ownerships a lot, to fix this there is something call SetNetworkOwner() you can set the part for only the server to take ownership, so for the npc you can loop through all its parts and set the ownership nil to stop lag

for _, v in pairs(NPC:GetChildren()) do
   if v:IsA("BasePart") then
      v:SetNetworkOwner(nil)
   end
end
6 Likes