Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Rotating pet where your head is pointing?

I am trying to make pet( the pet is model with primarypart) but, I need to make it rotate where you look. I don't know how and I am asking for help.

Code :

local Pet = script.Parent
local Move = script.Parent:WaitForChild('Move')
 
Move.OnServerEvent:connect(function(Player)
    if workspace[Player.Name] then
        local Head = workspace[Player.Name]:FindFirstChild('Head')
        if Head then
            Pet:SetPrimaryPartCFrame(CFrame.new(Head.CFrame.X + 1,Head.CFrame.Y +                                           2,Head.CFrame.Z + 1))
            Pet:SetPrimaryPartCFrame(Pet.Hitbox.CFrame * CFrame.Angles(0,Head.CFrame.lookVector.Z,0))
        end
    end
end)

Please help!

1 answer

✓ Accepted answer

Hi Sou,

You can make the head rotate easily by using the simple lookAt method. The lookAt method is basically using a CFrame with 2 values, one is the position it's staying and the other is the position it's looking at. So, it would be something like this:

local part_to_lookat = head.Position;
local pos_to_stay = primpart.CFrame.p;

primpart.CFrame = CFrame.new(pos_to_stay, pos_to_lookat);

###So in your code, it would look something like this:

local Pet = script.Parent
local Move = script.Parent:WaitForChild('Move')

Move.OnServerEvent:connect(function(Player)
    if workspace[Player.Name] then
        local Head = workspace[Player.Name]:FindFirstChild('Head')
        if Head then
            Pet:SetPrimaryPartCFrame(CFrame.new(Head.CFrame.X + 1,Head.CFrame.Y +                                           2,Head.CFrame.Z + 1))
            Pet:SetPrimaryPartCFrame(CFrame.new(Pet:GetPrimaryPartCFrame().p, Head.Position)); -- This is the changed place.
        end
    end
end)

##Hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

9 comments

0

Hello, thanks for help. But when I use this code it throws error "Unable to cast Vector3 to CoordinateFrame".

0

Oh wait, lemme fix that.

0

There, it should work now.

0

It stil doesn't work. No error but the pet is rotated wrongly, and it stays in the rotation and doesnt do anything else.

0

The pet is just frozen in wrong rotation

0

That's probably happening because the PrimaryPart's Front face is messed up. Make a new part and make it's front the front of the Pet. In Models, the sides don't act similarly, so you need to make sure you make your own front. https://www.youtube.com/watch?v=aHzfAoXdeXs&list=UUluMbZrto7C1iQVKMTXLGWw&index=20 This youtube video shows you how I made my own pet system.

0

Thanks very much.

0

No problem. Glad to help.

This discussion is preserved as it was. New answers, comments, and votes are closed.