How can I make my pets watching the HumanoidRootPart of the player?

Hey guys!

I’m trying to rotate my pets to make them look the player if he is IDLE.

I add this lines to my script :

char.Humanoid.Running:Connect(function(speed)
			if speed > 0 then
				-- Running --
				print("Running")
				for _, v in pairs(PlayerPets:GetChildren()) do
					local att = v.PrimaryPart:FindFirstChild("Attachment1")
					if att then
						att.CFrame = CFrame.new(att.Position) -- THIS IS CORRECT!!!
					end
				end
			else
				-- Idle --
				print("IDLE")
				for _, v in pairs(PlayerPets:GetChildren()) do
					local att = v.PrimaryPart:FindFirstChild("Attachment1")
					if att then
						att.CFrame = CFrame.new(att.Position, char.HumanoidRootPart.Position) -- This CFrame is WRONG!!!
					end
				end
			end
		end)

So my script triggers correctly when the player is IDLE or running but I have a problem with my CFrame.

The pets do not look the player when he is IDLE but when the player is running the pets’s CFrame is correct!

I would like to know how can I make them watch the HumanoidRootPart of the plr!

How can I make this?

4 Likes

Just to clarify, “IDLE” does print?

1 Like

Alright one moment. The only difference between these two pieces is the
char.HumanoidRootPart.Position
try printing that out itself, and see what prints.

Just a question. What exactly is attachment one?

It is the attachment of each pets for the AlignPosition / AlignOrientation.

Let me print(Char.HumanoidRootPart.Position)

image

So ^^
It prints the Position of my HumRP^^

I don’t understand why it doesn’t work because in the CFrame.new(This Vector3 is the position of the object, and this Vector3 is where the object will “lookAt”.

Just put the pet’s CFrame like (pet.Position, character.Root.Position). Utilise the lookAt parameter… I am not sure what you’re doing with the attachment stuff. I don’t understand its correlation with the pet.

1 Like

Ok thx I’ll try this and give you my feedback in a few Minutes

try

pet.CFrame = CFrame.new(pet.Position, HumanoidRootPart.Position)

this should angle the pet’s Front surface towards the humanoidRootPart

It doesn’t work because the pet’s CFrame is always changed by the AlignPosition’s Attachments.

It’s why I’m obliged to manipulate the CFrame of my Attachment :confused:

This is my actual Script :

local Moving = true
		char.Humanoid.Running:Connect(function(speed)
			if speed > 0 then
				if not Moving then
					Moving = true
					-- Running --
					print("Running")
					for _, v in pairs(PlayerPets:GetChildren()) do
						v.PrimaryPart.CFrame = CFrame.new(v.PrimaryPart.Position)
					end
				end
			else
				if Moving then
					Moving = false
					-- Idle --
					print("IDLE")
					for _, v in pairs(PlayerPets:GetChildren()) do
						v.PrimaryPart.CFrame = CFrame.new(v.PrimaryPart.Position, char.HumanoidRootPart.Position)
					end
				end
			end
		end)
	end)

The CFrame.new(origin,lookat) constructor was deprecated a few weeks ago, and is no longer supported. Here is a handy function that I found off minuteadrevenue’s twitter that does the exact same thing:

function LookAt(pos1,pos2)
    local LookVector = (pos1-pos2).Unit
    local ModelUpVector = Vector3.new(0,1,0)
    local RightVector = LookVector:Cross(ModelUpVector)
    local UpVector = RightVector:Cross(LookVector)
    
    return CFrame.fromMatrix(pos1,RightVector,UpVector,LookVector)
end

Hey thank you for your awnser!

So if I run this function with the Vector3(attachment.Position) and the Vector(char.HumanoidRootPart.Position) it should return the correct CFrame and my pets will look the player?

Yes.
It still supported, and still works.
And will not probably break

I’m not sure about what I am doing, so does it have to look like this ?

local Moving = true
		char.Humanoid.Running:Connect(function(speed)
			if speed > 0 then
				if not Moving then
					Moving = true
					-- Running --
					print("Running")
					for _, v in pairs(PlayerPets:GetChildren()) do
						local att = v.PrimaryPart:FindFirstChild("Attachment1")
						if att then
							att.CFrame = CFrame.new(att.Position)
						end
					end
				end
			else
				if Moving then
					Moving = false
					-- Idle --
					print("IDLE")
					for _, v in pairs(PlayerPets:GetChildren()) do
						local att = v.PrimaryPart:FindFirstChild("Attachment1")
						if att then
							att.CFrame = LookAt(att.Position, char.HumanoidRootPart.Position)
						end
					end
				end
			end
		end)

This script makes my pets facing behind them…

So when the player is moving they are facing in front (where the player is looking at) and when the player is IDLE the pets are looking behind (in the back of the plr)

Try doing

att.CFrame = -(LookAt(att.Position, char.HumanoidRootPart.Position))

I have no clue if this will work, but making it negative should make it face the opposite way.

It doesn’t work at all…

  14:28:44.821 - ServerScriptService.MainScripts.MainHolder:347: attempt to perform arithmetic (unm) on CFrame
14:28:44.822 - Stack Begin
14:28:44.823 - Script 'ServerScriptService.MainScripts.MainHolder', Line 347
14:28:44.824 - Stack End

I think I find why have this issues!

It probably come from the alignOrientation.

When I tried to change the rotation of the CFrame by myself it stayed to 0,0,0 so I think it come from the AlignOrientation.

How can I change this?