Making the player's head always face a part infront of it

Hello!
I want to weld a part to the player’s head and position the part infront of the player. I also want to make the player’s head to always face at the part infront of it.

I found a script on here which makes the player’s head always face a part. But if I put it together with the part maker script then the head just spins infinitely.

local char = script.Parent
local newPart = Instance.new("Part")
newPart.CanCollide = false
newPart.Transparency = 0
newPart.Size = Vector3.new(1,1,1)
newPart.Parent = char
local hrp = char.HumanoidRootPart
local Head = script.Parent:WaitForChild("Head")
local weld = Instance.new("WeldConstraint")
weld.Part0 = newPart
weld.Part1 = Head
weld.Parent = newPart
newPart.Position = Vector3.new(hrp.Position.X, hrp.Position.Y + 0.25, hrp.Position.Z - 4)

local target = script.Parent:WaitForChild("Part")
local neckJoint = Head:WaitForChild("Neck")
local upperTorso = neckJoint.Part0

local function worldCFrameToC0ObjectSpace(motor6DJoint,worldCFrame)
	local part1CF = motor6DJoint.Part1.CFrame
	local c1Store = motor6DJoint.C1
	local c0Store = motor6DJoint.C0
	local relativeToPart1 =c0Store*c1Store:Inverse()*part1CF:Inverse()*worldCFrame*c1Store
	relativeToPart1 -= relativeToPart1.Position

	local goalC0CFrame = relativeToPart1+c0Store.Position
	return goalC0CFrame
end

while task.wait() do
	local goalCF = CFrame.lookAt(Head.Position, target.Position, upperTorso.CFrame.UpVector)
	neckJoint.C0 = worldCFrameToC0ObjectSpace(neckJoint,goalCF)
end
3 Likes