So I’m trying to make an NPC face the player when they’re near. However, that moves the position of the head as well, and it’s very unnatural.
Anchoring the part fixes its position, but it won’t rotate; unanchoring the head produces this result.
My script essentially sets the Attachment1 of the AlignOrientation object to a specific attachment on the player when they enter the zone, and sets Attachment1 to nil when there are no players inside the zone.
Here’s my script:
local alignment = script.Parent.Parent.Guard.AlignOrientation
local zone = script.Parent
local list = {}
function addWithoutDuplicate(value)
for _, v in list do
if v == value then
return
end
end
table.insert(list, value)
end
function remove(value)
for i, v in list do
if v == value then
table.remove(list, i)
return
end
end
end
zone.Touched:Connect(function(part)
if part:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Head.FaceFrontAttachment
addWithoutDuplicate(part)
elseif part.Parent:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Parent.Head.FaceFrontAttachment
addWithoutDuplicate(part.Parent)
elseif part.Parent.Parent:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Parent.Parent.Head.FaceFrontAttachment
addWithoutDuplicate(part.Parent.Parent)
elseif part.Parent.Parent.Parent:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Parent.Parent.Parent.Head.FaceFrontAttachment
addWithoutDuplicate(part.Parent.Parent.Parent)
end
if #list == 0 then
alignment.Attachment1 = nil
end
end)
zone.TouchEnded:Connect(function(part)
if part:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Head.FaceFrontAttachment
remove(part)
elseif part.Parent:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Parent.Head.FaceFrontAttachment
remove(part.Parent)
elseif part.Parent.Parent:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Parent.Parent.Head.FaceFrontAttachment
remove(part.Parent.Parent)
elseif part.Parent.Parent.Parent:FindFirstChild("Humanoid") then
alignment.Attachment1 = part.Parent.Parent.Parent.Head.FaceFrontAttachment
remove(part.Parent.Parent.Parent)
end
if #list == 0 then
alignment.Attachment1 = nil
end
end)