Enemies jittering when player is out of range

I am trying to create a pathfinding enemy, however I am coming across this animation bug. I am following an enemy ai tutorial and know how it works and have implemented it into my game. The only issue is that at some point the enemy, even while the player is out of range, starts jittering like so:

28ebcbedab79b8dfb8dc6272cd17a32b

I don’t really know why this happens or whether or not its a movement issue or an animation issue. When disabling the animations, it seems to work fine however I was thinking it could have something to do with the humanoid:Move function I’m using for enemy movement. It might also be because enemy movement is handled on a server script while animation is handled on a local script.

If anyone has an idea on why this may happen please reply.
Thanks.

Note: After doing some further testing, it appears that after some time, all other enemies on the map end up jittering and even start to move a significant distance. Therefore I believe that this may be a movement script issue. I’ll post the full movement script below. I have also been experiencing several animation bugs involving jittering recently so I don’t exactly know if this is really the issue.

local remoteAnimEvent = game:GetService('ReplicatedStorage'):WaitForChild("Events")["RemoteAnimationEvent"]

local statue = script.Parent
local statueRoot = statue:WaitForChild("HumanoidRootPart")
local statueHum = statue:WaitForChild("Humanoid")

statueHum.BreakJointsOnDeath = false

local defSpeed = statueHum.WalkSpeed

local activationRadius = statue:WaitForChild("activationRadius")

local stopDist = 5
local attackDist = 8
local lastAttack = tick()

local function findPlr() -- just looks for the nearest character
	local plrList = game.Players:GetPlayers()
	
	local nearestPlr
	local dist
	local dir
	
	for _,plr in pairs(plrList) do
		local chr = plr.Character
		
		if chr then
			local distVector = chr:WaitForChild("HumanoidRootPart").Position - statueRoot.Position

			if not nearestPlr then
				nearestPlr = plr
				dist = distVector.Magnitude
				dir = distVector.Unit
				
			--	print("a")

			elseif distVector.Magnitude < dist then
				nearestPlr = plr
				dist = distVector.Magnitude
				dir = distVector.Unit
				
			--	print("b")

			end
			
		end
	end
	
	return nearestPlr, dist, dir
end

game:GetService("RunService").Heartbeat:Connect(function()
	--print("working")

	local nearestPlr, dist, dir = findPlr()

	if nearestPlr then

		if dist <= activationRadius.Value and dist >= stopDist then
			statueHum:Move(dir)
		else
			statueHum:Move(Vector3.new())
		end

		if dist <= attackDist and tick() - lastAttack >= 2 then

			lastAttack = tick()

			remoteAnimEvent:FireAllClients(statue, nearestPlr) --event I'm using to relay information about animations from the server side to all clients

		end
	end
	
end)

I’m dumb. I forgot to disable collisions for the spear
I’ll close the question now.

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