NPC strafe jittering

I cannot find out why it is jittering like this, it would work fine the first 10 to 15 strafe, 20 if i was lucky. it just happens, and i cannot tell WHY.

normal, working strafing:
No jitter

bugged strafing:
Jitter

Module script:

local ServerStorage = game:GetService("ServerStorage")

local Strafe = {}

local function getAdjacentVertices(NPCPosition, PlayerPosition, VertexCount, Radius)
	local center = PlayerPosition
	local npc = NPCPosition
	local direction = (npc - center)
	local currentAngle = math.atan2(direction.Z, direction.X)
	local angleStep = (2 * math.pi) / VertexCount
	local angle1 = currentAngle + angleStep
	local angle2 = currentAngle - angleStep
	local vertex1 = center + Vector3.new(math.cos(angle1), 0, math.sin(angle1)) * Radius
	local vertex2 = center + Vector3.new(math.cos(angle2), 0, math.sin(angle2)) * Radius

	return vertex1, vertex2
end

local function waitForArrival(RootPart, Destination, maxTime, tolerance)
	maxTime = maxTime or 2
	tolerance = tolerance or 0.5

	local timer = 0
	while timer < maxTime do
		local distance = (RootPart.Position - Destination).Magnitude
		if distance <= tolerance then
			return true -- arrived
		end
		timer += task.wait()
	end
	return false -- timed out
end

function Strafe.Strafe(Character, Target, StrafeData)
	
	local Vertex = StrafeData.Vertex
	local Direction = StrafeData.Direction
	local Radius = StrafeData.Radius
	local Steps = StrafeData.Steps
	
	print(StrafeData.Steps, StrafeData.Direction)
	
	local Humanoid = Character.Humanoid
	
	if Character:GetAttribute("Strafe") == true then
		Character:SetAttribute("Strafe", false)
		Humanoid:MoveTo(Character.HumanoidRootPart.Position)
	end
	
	Character:SetAttribute("Strafe", true)
	for i = 1, Steps do
		--print(Direction, Steps)
		local Left, Right = getAdjacentVertices(Character.HumanoidRootPart.Position, Target.HumanoidRootPart.Position, Vertex, Radius)
		local Destination
		
		if Direction == 0 then
			Destination = Vector3.new(Left.X, Character.HumanoidRootPart.Position.Y ,Left.Z)
		else
			Destination = Vector3.new(Right.X, Character.HumanoidRootPart.Position.Y ,Right.Z)
		end
		warn("NewCalc")
		workspace.Terrain.Target.Position = Destination
		Humanoid:MoveTo(Destination)
		
		
		
		local Conenction
		local StepReached = false
		local Timeout = 0
		local success = waitForArrival(Character.HumanoidRootPart, Destination, 2, 1)
		if not success then
			warn("Strafe step timeout")
		end
		warn("StrafePassed")
		Humanoid:MoveTo(Character.HumanoidRootPart.Position)
	end
	Character:SetAttribute("Strafe", false)
end

--[[
StrafeData = {
	Vertex = 20,
	Direction = math.random(1,0),
	Radius = 6,
	Steps = math.random(1, 20)
}]]

return Strafe

please help.

strafing jitters because you’re repeatedly stopping and restarting the NPC’s movement each step,
and MoveTo can glitch if the client owns the network for the HumanoidRootPart,
stop cancelling movement and remove the final;

Humanoid:MoveTo(Character.HumanoidRootPart.Position)

at the end of each loop so the NPC doesn’t stop and restart,
then lock network ownership to the server in a server script;

Character.HumanoidRootPart:SetNetworkOwner(nil)

so movement updates aren’t jittery on clients
instead of fixed MoveTo steps, use PathfindingService or do a quick magnitude check to advance when the NPC is close enough to avoid stops at every vertex

when you mentioned this, i checked the viewport option myself, and it is because of network ownership, thank you very much :blush:.

You’re very welcome, glad I could help! :happy4:

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