Npc's movement is very laggy and jittery

The movement I used for this NPC is MoveTo in a while loop but it looks pretty glitchy and not smooth. Is there any other alternative movement methods for my NPC so that it can follow players smoothly?

Set the network owner of the NPCā€™s PrimaryPart (the HumanoidRootPart if it is r15, the Head if it is r6) to nil. This will set it to the server and remove the jittering.

2 Likes

That didnā€™t method didnā€™t really work out for me, I just tried it but it is still glitchy and laggy. Another issue is that when I set the npcā€™s speed to 20 the npc somehow cannot catch up to my character

Can I see your full code?
charscharschars

ā€˜ā€™ā€™
local pathService = game:GetService(ā€œPathfindingServiceā€)

local myHum = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")

myRoot:SetNetworkOwner(nil)

function findTarget()
	local dist = 200
	local target
	
	for i,v in ipairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v.Name ~= script.Parent.Name then
			local hum = v:FindFirstChild("Humanoid")
			local root = v:FindFirstChild("HumanoidRootPart")
			
			if (myRoot.Position - root.Position).magnitude <= 200 then
				dist = (myRoot.Position - root.Position).magnitude
				target = v
			end
		end
	end
	return target
end

function attack(target)
	local hum = target.Humanoid
	local root = target.HumanoidRootPart
	
	if (myRoot.Position - root.Position).magnitude > 10 then
		myHum:MoveTo(root.Position)
	else
		myHum:MoveTo(root.Position)
		hum:TakeDamage(5)
	end

end

function walkTo(destination)
	local path = pathService:CreatePath()
	path:ComputeAsync(myRoot.Position,destination)
	
	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		for i,waypoint in pairs(waypoints) do
			local target = findTarget()
			if target then
				print(target.Name,"Found")
				attack(target)
				break
			else
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					myHum.Jump = true
				end
				myHum:MoveTo(waypoint.Position)
				myHum.MoveToFinished:Wait()

			end
		end
	else
		print("Failed to compute path")
		walkTo(destination)
	end
end

function patrol()
	local xRandom = math.random(-50,50)
	local zRandom = math.random(-50,50)
	local posRandom  = myRoot.Position + Vector3.new(xRandom,0,zRandom)
	
	print(posRandom)
	walkTo(posRandom)
end

while wait(.5)do
	patrol()
end

ā€˜ā€™ā€™


This is what i mean by npc canā€™t catch up to the character

Thats probably because its too slow. Everytime it gets to the players previous position it stops and the player is not there anymore. Try making him not stop. Also lag could be the problem try using this

game:GetService("RunService").Heartbeat:Connect(function(deltatime)
        patrol()
end)
1 Like

Ok i put the while loop to wait .5 thatā€™s why thx

2 Likes

you can also just do lua task.wait() with nothing inside the wait and the while true do loop will fire it as fast as possible
but id use heartbeat

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