Tweening a NPC Zombie makes it fling on the server

Hello, I have made a script that tweens the NPC to the next waypoint. But the problem is whenever I switch to the server view, or when a new player joins in-game, the zombies glitch until they reach the final waypoint and get destroyed (The new ones work). But how could I fix this problem? I am currently tweening the HumanoidRootPart on the server, and all of the parts of the zombies are Massless.

The code in a module script:


local uniqueSeed = Random.new()
	
	task.spawn(function()
		
		zombie:PivotTo(CFrame.new(workspace.Spawn.Position.X, workspace.Spawn.Position.Y + zombie:FindFirstChild("Height").Value, workspace.Spawn.Position.Z))
		
		local hrp = zombie:FindFirstChild("HumanoidRootPart")
		
		local ZOMBIE_OFFSET = uniqueSeed:NextNumber(-0.34, 0.34)

		local WalkSpeed = zombie:FindFirstChild("WalkSpeed")

		local Animator = zombie:WaitForChild("AnimationController"):WaitForChild("Animator")
		
		local anim = Instance.new("Animation")
		anim.AnimationId = "rbxassetid://14755070010"
		anim.Parent = Animator

		local animation = Animator:LoadAnimation(anim)
		
		animation.Looped = true
		animation:Play()
		animation:AdjustSpeed(1 * WalkSpeed.Value/3.9)

		for waypoint = 1, #waypoints:GetChildren() do
			
			task.wait()
			
			local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude

			local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))

			local rx, ry, rz = cframeLookat:ToOrientation()
			local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)

			local orientation = Vector3.new(dx, dy, dz)

			local Time = GetTime(Distance, WalkSpeed.Value)

			local moveTween = TweenService:Create(hrp, TweenInfo.new(Time*1.25, Enum.EasingStyle.Linear), {Position = Vector3.new(waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2), hrp.Position.Y, waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2))}) 
			moveTween:Play()
	
			local orientationTween = TweenService:Create(hrp, TweenInfo.new(Time/Distance*3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Orientation = orientation})
			orientationTween:Play()

			moveTween.Completed:Wait()
			
		end
		
		zombie:Destroy()
		
	end)

I don’t exactly know what the problem is but it could be a serious issue in the future if a player joined late and saw this. ((This system has no Humanoids so don’t suggest anything with MoveTo() or Humanoid properties!!))

I dont know if this will fix anything, but a suggestion for the look system is: calculate the crframe using all x,y,z values of the waypoint, and then set only the y rotation on the zombie. If you set the x or the z you get tilting.

2 Likes

Have you tried to use MoveTo() and MoveToFinished:Wait() on the zombie’s humanoid? It may not be smooth though.

Simply use MoveTo doesn’t the NPCs have humanoids ? :smiley:

Also i would recommend that you learn OOP for better performance and better handling!

I am currently making a no humanoid system because they have outdated code and are kind of lame. MoveTo is also kind of performance heavy while having 40-60 zombies at once.

The zombies current state

The system is working pretty well but there is still flinging on the server view and when a new player joins.