How to make tweening across server/client less laggy

Hi, for my game I have non-humanoid NPCs. To move them on the server I call a remote tween it and once the tween time has passed I move it to the waypoint on the server. Is there a better way to go about this or make it less laggy?

----------- Client ------------
tweenRemote.OnClientEvent:Connect(function(self,tweenTime,position)
	if self._running then
		tween = TweenService:Create(self.Npc.PrimaryPart,TweenInfo.new(tweenTime,Enum.EasingStyle.Linear),{Position = position})
		local lookAt = CFrame.lookAt(self.Npc.PrimaryPart.Position,Vector3.new(position.X,self.Npc.PrimaryPart.Position.Y,position.Z))
		turningtween = TweenService:Create(self.Npc.PrimaryPart,TweenInfo.new(0.2,Enum.EasingStyle.Linear),{CFrame = lookAt})
		tween:Play()
		turningtween:Play()
	elseif not self._running then
		if tween then
			tween:Cancel()
		end
		if turningtween then
			turningtween:Cancel()
		end
	end
end)
---------------------Server---------------
connection = RunService.Heartbeat:Connect(function()
	if self._running then
		if nextWaypointIndex <= #waypoints then
			if not tween then
				tween = true
				oldPosition = self.Npc.PrimaryPart.Position
				waitTime = (self.Npc.PrimaryPart.Position - waypoints[nextWaypointIndex].Position).Magnitude/self.walkspeed
				tweenRemote:FireAllClients(self,waitTime,waypoints[nextWaypointIndex].Position + Vector3.new(0,self.Npc.PrimaryPart.Size.Y/2))
				lastTime = os.clock() + waitTime
			end
			if lastTime - os.clock() <= 0 then
				if tween then
					local lookAt = CFrame.lookAt(oldPosition,Vector3.new(waypoints[nextWaypointIndex].Position.X,self.Npc.PrimaryPart.Position.Y,waypoints[nextWaypointIndex].Position.Z))
					self.Npc.PrimaryPart.CFrame = CFrame.new(waypoints[nextWaypointIndex].Position + Vector3.new(0,self.Npc.PrimaryPart.Size.Y/2)) * lookAt.Rotation
					nextWaypointIndex += 1
					tween = false
				end
			end
		else
			connection:Disconnect()
			blockedConnection:Disconnect()
		end
	else
		connection:Disconnect()
		blockedConnection:Disconnect()
	end
end)

Hello @ninjamaster5355

You can always move it on every client you can do that just by calling from the server
https://developer.roblox.com/en-us/api-reference/function/RemoteEvent/FireAllClients

Then once it reaches its destination the server simply moves it by setting it with its position, if is a model you can use:
https://developer.roblox.com/en-us/api-reference/function/Model/SetPrimaryPartCFrame

1 Like

Hey, sorry for the late reply I was busy. In my original post that’s what I said I was doing. I was asking if there was a better way to move it. Thanks for the reply though.

You could try using TweenService V2.

1 Like

Thanks, but I ended up with alignposition because it replicates nicely.

1 Like