Players head position with pets

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a pet’s position go opposite of the player’s head position. Essentially I want the pets position to be always behind the player

  2. What is the issue? Include screenshots / videos if possible!
    I’ve got it to somewhat work with, but when I look directly at the pet It doesn’t move.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve discussed with a friend and they didn’t have a solution either

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

The pet system works, but I’m confused on how to go further with the system

local equipPetEvent = game.ReplicatedStorage.equipPet

local petPosition = {}

local TweenService = game:GetService("TweenService")

equipPetEvent.OnServerEvent:Connect(function(plr, nameOfPet)
	for i, v in pairs(game.ServerStorage:GetChildren()) do
		if v.Name == nameOfPet then
			local clonedPet = v:Clone()
			clonedPet.Parent = workspace
			table.insert(petPosition, clonedPet)
			local increment = #petPosition
			while wait() do
				local newPetTween = TweenService:Create(clonedPet, TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Position = plr.Character.HumanoidRootPart.Position - Vector3.new(-5 * increment, 0, 0)})
				newPetTween:Play()
			end
		end
		print("Entered text does not match any pets")
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Alright. So the thing you want help with is probably this line:

local newPetTween = TweenService:Create(clonedPet, TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Position = plr.Character.HumanoidRootPart.Position - Vector3.new(-5 * increment, 0, 0)})

More specifically this part

 {Position = plr.Character.HumanoidRootPart.Position - Vector3.new(-5 * increment, 0, 0)})

This is a solid first attempt at this. The problem is that you are just moving it on the X axis. So if the player is facing the negative direction on the X axis, it will be facing the pet.

The solution to this is to actually use the direction the HumanoidRootPart is facing. CFrames have a nice little property called .LookVector which tells you which direction it is facing. You can use that instead.

{Position = plr.Character.HumanoidRootPart.Position - plr.Character.HumanoidRootPart.CFrame.LookVector * 5 * increment})

If you stick that in your code in place of the old one it should work. LookVector will give the direction that HumanoidRootPart is facing with a length of 1 so you need to multiply it by desired distance.

A couple things about your code worth noting, but not directly related to your problem.

This

for i, v in pairs(game.ServerStorage:GetChildren()) do
		if v.Name == nameOfPet then

Can be replaced with this in most situations

local pet = game.ServerStorage:FindFirstChild(nameOfPet)
if pet then --See if it found a child named nameOfPet in ServerStorage
  --The rest of your code goes here
  local clonedPet = pet:Clone()
  --...
end

I find it just tends to look cleaner this way. It technically is doing the same thing however.

Another note is that how you currently have this set up means that you will never leave that loop without an error

while wait() do
	local newPetTween = TweenService:Create(clonedPet, TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Position = plr.Character.HumanoidRootPart.Position - Vector3.new(-5 * increment, 0, 0)})
	newPetTween:Play()
end

If you ever want to delete that pet, this loop won’t know about it. There are 2 ways you can fix this. You can set a variable that tracks if you still need to update the pet, or you could do this

while clonedPet do
	local newPetTween = TweenService:Create(clonedPet, TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Position = plr.Character.HumanoidRootPart.Position - Vector3.new(-5 * increment, 0, 0)})
	newPetTween:Play()
    wait()
end

With this code it will automatically stop looping when you delete your pet instead of giving an error.