How can I rotate model to face player on X axis?

So currently I have a script which rotates the entire model with a while loop but I need it so when I run it the PrimaryPart on the X axis rotates once to the player’s direction
(I’m not pro at CFrames)

while true do
script.Parent:SetPrimaryPartCFrame(script.Parent:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,.05,0))
wait()
end
2 Likes

To start, do not use a while true do wait() loop, instead use RunService.Stepped. A while loop in this case would just add stress to your system, while accomplishing the same thing as Stepped, which runs every frame.

So you would start with:

local RunService = game:GetService("RunService") 
local model = script.Parent 
local part = script.Parent.PrimaryPart 

RunService.Stepped:Connect(function()
    --This runs every frame 
end) 

Next, you would have to get the CFrame that starts at the model and looks at the player

local charPos =player.Character.Position 
local cframe = CFrame.lookAt(part.Position, charPos)

You would then have to get the YVector from that cframe, and create a new one so it is only rotated on the X axis

local modelCFrame = CFrame.fromMatrix(part.Position, part.CFrame.XVector, cframe.YVector) 

Finally, you would apply this cframe to the model:

model:SetPrimaryPartCFrame(modelCFrame)

So your final script would be:

local RunService = game:GetService("RunService") 
local model = script.Parent 
local part = script.Parent.PrimaryPart 

RunService.Stepped:Connect(function()
    --This runs every frame 
    local charPos = player.Character.Position 
    local cframe = CFrame.lookAt(part.Position, charPos)

    local modelCFrame = CFrame.fromMatrix(part.Position, part.CFrame.XVector, cframe.YVector) 

    model:SetPrimaryPartCFrame(modelCFrame)
end) 


4 Likes

The script works but the part doesn’t follow my X axis and instead just like flips side to side

Ok I made modification to the thing an now it follows you how I need but it now just spins around constantly than stop at 1 direction

local modelCFrame = CFrame.fromMatrix(part.Position, part.CFrame.ZVector, cframe.YVector) 

That is your X axis. Do you want it to follow by your Y axis?

I fixed it I just did

local modelCFrame = CFrame.fromMatrix(part.Position, cframe.XVector, part.CFrame.YVector,  cframe.ZVector) 
2 Likes

I am trying to do something like this. I have a model I want to always face every player. I think I need to do this on the client side but I am having trouble wrapping my head around where to build the script.