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
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)
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.