Pet Following Player Lagging

Hello, I am trying to make a pet follow the player, it works perfectly, except when I have a part that is rotating on the pet. If i remove the rotation script, it works perfectly.
This is my script to make it follow the player.

local pet = workspace:WaitForChild("Template")
local runService = game:GetService("RunService")

function givePet(player)

	if player then
		local character = player.Character
		if character then

			local humRootPart = character.HumanoidRootPart
			local newPet = pet:Clone()
			newPet.Name = "Pet"
			newPet.Parent = game.Workspace
			
			local head = newPet.Head
			head.Anchored = false
			
			head:SetNetworkOwner(player)
			newPet.Parent = character
			
			
			local bodyPos = Instance.new("BodyPosition", head)
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			
			local bodyGyro = Instance.new("BodyGyro", head)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
			
			local rv = bodyGyro.CFrame.rightVector
			
			while true do
				bodyPos.Position = humRootPart.Position + Vector3.new(1, 1, 2)
				bodyGyro.CFrame = humRootPart.CFrame * CFrame.Angles(0, math.rad(90), 0)
				runService.Stepped:Wait()
		
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		givePet(player)
	end)
end)

This is the rotation script

wait(2)
local RunService = game:GetService("RunService")
local character = script.Parent.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")

local part = script.Parent

RunService.Stepped:Connect(function()
	if humanoid.MoveDirection.Magnitude == 0 then
		part.Orientation = part.Orientation + Vector3.new(0, 5, 0)
		
	else
		part.Orientation = part.Orientation + Vector3.new(0, 0, 0)
	end
end)

I can’t use physics to rotate it because it is welded to the pet, so the whole pet would rotate, which is not what I am looking for.

Thanks :slight_smile:

For the BodyGyro try using the CFrame.lookAt() method

Would that make the whole pet rotate or only the part, since it is welded to the pet?

If your pet has a primarypart/root then yes the whole pet would rotate.

The whole pet rotates with it, even though there is no root part / primary part. This is my code.

local RunService = game:GetService("RunService")
local character = script.Parent.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")

local part = script.Parent

RunService.Stepped:Connect(function()
	if humanoid.MoveDirection.Magnitude == 0 then

		part.CFrame = CFrame.lookAt(part.Position, part.Orientation + Vector3.new(0, 5, 0))
		
	else
		part.Orientation = part.Orientation + Vector3.new(0, 0, 0)
	end
end)