Hello! I’m currently making a game and I want to add a part that looks at the player ONLY by using the Y axis, just like how a Beam would do (a part that only rotates to left and right but not up or down). Since this game is going to be First - Person, I want the part to follow the Player’s Head so it actually looks like it’s looking at you. I’m unsure how I would do that since I’m really bad at coding so I appreciate any help.
Maybe something like this?
local look = CFrame.new(part.Position, player.Character.Head.Position)
local x, y, z = look:ToOrientation()
part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0,math.rad(y),0)
It did nothing. The part was completely still.
I changed the code, maybe it is fixed. You would need to put that code in a RenderStepped event though.
Could you give an example of how it would be if it’s possible? I’m not really familiar with RenderStepped so I’m unsure about how I would go about it.
This will make the code run every frame, on the client.
game:GetService("RunService").RenderStepped:Connect(function()
local look = CFrame.new(part.Position, player.Character.Head.Position)
local x, y, z = look:ToOentation()
part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0,math.rad(y),0)
end)
If you want it on the server, you would use .Stepped
instead. But I don’t recommend that because it could lag.
It still does nothing unfortunatly.
I might’ve found something wait a min.
Are there any errors in the output? Also I noticed I made a typo somehow and I fixed that.
I’ve mixed up StarterCharacterScripts and StarterPlayerScripts turns out so that’s why it wasn’t doing anything. I’ve already fixed the misspells though now it gives this error:
Players.Fireboy0122.PlayerScripts.LocalScript:5: attempt to index nil with 'Head'
This has some if statements which should fix that error:
local player = game.Players.LocalPlayer
local part -- define this variable
game:GetService("RunService").RenderStepped:Connect(function()
if player.Character and player.Character:FindFirstChild("Head") and part.Parent then
local look = CFrame.new(part.Position, player.Character.Head.Position)
local x, y, z = look:ToOrientation()
part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0,math.rad(y),0)
end
end)
I see, the ri
in Orientation moved infront of CFrame.new
…
Sorry for the late response I had to do something. I’ve tested the script and it did nothing.
The problem seems like it’s about the code that makes the part look at the player, not the actual detection of the parts used in the code if that helps in any way?