How do i make the player`s head look at the mouse pointer

can you guys help me make the characters head look at the mouse pointer. i made that the whole character looks at the mouse pointer but i only want the head to look

this is the code i made

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local Root = player.Character.HumanoidRootPart

while true do
	wait()
	local RootPos, MousePos = Root.Position, mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end

if you know how pls help

5 Likes

Instead of using the HumanoidRootPart make a variable that refers to the head and do the same.

1 Like

i tried but then my character just gliched a few times, but it still is the same as humanoidrootpart

1 Like

That might be because the mouse doesnt have a Z position by default, did you convert the mouse to 3d space?

1 Like

nope, it didn`t work, this time it was the same just your whole body just turned to the ground

1 Like

i have old code that does that but with the camera lookvector

theplace = Plr.Character.Torso.CFrame:toObjectSpace(camera.CFrame).lookVector
char.torso.neck.c0 =   CFrame.new(0,1.025,0) * CFrame.Angles(math.tan   (theplace.Y)+math.rad(-90),0,math.acos   (theplace.X)+math.rad(90))

so just replace some of the code

what is the camera in the first line?

1 Like

got it working

theplace = player.Character.Torso.CFrame:toObjectSpace(player:GetMouse().Hit.lookVector
player.Character.Torso.Neck.C0 =   CFrame.new(0,1.025,0) * CFrame.Angles(math.tan   (theplace.Y)+math.rad(-90),0,math.acos   (theplace.X)+math.rad(90))
3 Likes

do you have the full code, there are some errors i dont know how to fix

this is the full code to the local script
i have a script that just plays a tween with the given cframe

local Plr = game.Players.LocalPlayer
local neck = Plr.Character:WaitForChild("Torso"):WaitForChild("Neck")
local camera = workspace.CurrentCamera

--local hip = Plr.Character.Torso:WaitForChild("Right Hip")
local humanoid = Plr.Character.Humanoid
local theplace = nil


while wait(.075) do
	local mouse = Plr:GetMouse()
	theplace = Plr.Character.Torso.CFrame:toObjectSpace(camera.CFrame).lookVector
	local value1 = CFrame.new(0,1.025,0) * CFrame.Angles(math.tan   (theplace.Y)+math.rad(-90),0,math.acos   (theplace.X)+math.rad(90))
	if humanoid.Health == 0 then
		value1 =CFrame.new(0,1.025,0) * CFrame.Angles(math.rad(0),0,math.rad(90))
		
		
		game.ReplicatedStorage.HeadLook:FireServer(value1)
	else
		
		
		
		if script.Parent.Running.Value == false then
			--theplace = Plr.Character.Torso.CFrame:toObjectSpace(mouse.Hit).lookVector
			
			--this is what you need
			theplace = Plr.Character.HumanoidRootPart.CFrame:toObjectSpace(camera.CFrame).lookVector
			value1 = CFrame.new(0,1.025,0) * CFrame.Angles(math.tan   (theplace.Y)+math.rad(-90),0,math.acos   (theplace.X)+math.rad(90))
			
			
		else
			theplace = Plr.Character.HumanoidRootPart.CFrame:toObjectSpace(camera.CFrame).lookVector
			value1 = CFrame.new(0,1.025,0) * CFrame.Angles(math.asin   (theplace.Y)+math.rad(-90),0,math.acos   (theplace.X)+math.rad(90))
		end

		local value = CFrame.Angles(-math.asin(theplace.Y)+5,0,math.asin(theplace.X)+84.75)
--print(value1)
		--84.75
		--neck.C0 = value1
		game.ReplicatedStorage.HeadLook:FireServer(value1)
		
		
	end
end
2 Likes

still i think you only need this

local Plr = game.Players.LocalPlayer
local mouse = Plr:GetMouse()
theplace = Plr.Character.Torso.CFrame:toObjectSpace(mouse.Hit).lookVector
value1 = CFrame.new(0,1.025,0) * CFrame.Angles(math.tan   (theplace.Y)+math.rad(-90),0,math.acos   (theplace.X)+math.rad(90))

2 Likes

im going to look at the code later. because i gtg

it didn`t work, nothing happened when i used the code

in using R6 btw maybe that´t why it doesn´t work?

Assuming you just want the character’s head to rotate around the Y-axis. The following is compatible with both avatar types.

do
	local Game = game
	local RunService = Game:GetService("RunService")
	local Players = Game:GetService("Players")
	local Player = Players.LocalPlayer
	local Mouse = Player:GetMouse()
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
	local RigType = Humanoid.RigType.Name
	local Limb = if RigType == "R6" then Character:FindFirstChild("Torso") or Character:WaitForChild("Torso") elseif RigType == "R15" then Character:FindFirstChild("Head") or Character:WaitForChild("Head") else nil
	if not Limb then return end
	local Joint = Limb:FindFirstChild("Neck") or Limb:WaitForChild("Neck")

	local function OnRenderStep()
		Joint.C0 = CFrame.lookAt(Joint.C0.Position, Vector3.new(Mouse.Hit.Position.X, Joint.C0.Position.Y, Mouse.Hit.Position.Z)) * if RigType == "R6" then CFrame.Angles(math.pi / 2, math.pi, 0) else CFrame.Angles(0, 0, 0)
	end

	RunService.RenderStepped:Connect(OnRenderStep)
end
2 Likes

hey, i was wondering can you make so the head dont turn all the way around?

Would require a bit of clamping.

do
	local Game = game
	local RunService = Game:GetService("RunService")
	local Players = Game:GetService("Players")
	local Player = Players.LocalPlayer
	local Mouse = Player:GetMouse()
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
	local RigType = Humanoid.RigType.Name
	local Limb = if RigType == "R6" then Character:FindFirstChild("Torso") or Character:WaitForChild("Torso") elseif RigType == "R15" then Character:FindFirstChild("Head") or Character:WaitForChild("Head") else nil
	if not Limb then return end
	local Joint = Limb:FindFirstChild("Neck") or Limb:WaitForChild("Neck")

	local function OnRenderStep()
		Joint.C0 = CFrame.lookAt(Joint.C0.Position, Vector3.new(Mouse.Hit.Position.X, Joint.C0.Position.Y, math.min(-Joint.C0.LookVector.Z, Mouse.Hit.Position.Z))) * if RigType == "R6" then CFrame.Angles(math.pi / 2, math.pi, 0) else CFrame.Angles(0, 0, 0)
	end

	RunService.RenderStepped:Connect(OnRenderStep)
end
7 Likes