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!
Alright one moment. The only difference between these two pieces is the char.HumanoidRootPart.Position
try printing that out itself, and see what prints.
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.
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
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?
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)
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)
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