NPC look at player (smoothly)

hello fellow developers
i am trying to make the npc look at me when they are close, but when i try to do it they get very wonky
robloxapp-20220608-1836467.wmv
how can i make my npc look at me, in a smooth way?

crithitbox.Touched:Connect(function(p)
			if p.Parent:FindFirstChild("Humanoid") then
				local ff = game:GetService("Players"):GetPlayerFromCharacter(p.Parent)
				if ff ~= nil then
					lookat = true
					script.Parent:SetPrimaryPartCFrame(CFrame.lookAt(script.Parent.HumanoidRootPart.Position, p.Parent.HumanoidRootPart.Position, p.Parent.HumanoidRootPart.Orientation + Vector3.new(180, 0, 0)))
					wait(1)
				end
			else
				lookat = false
			end
		end)
for people who want to see the whole unfinished 96 line script
--Items
local folder = script.Parent:WaitForChild("Animations")
local Idle = folder:WaitForChild("Idle")
local target = nil
local rpm = 780 / 60000
local pfs = game:GetService("PathfindingService")
local det = false
local lookat = false
local bullet = game:GetService("ServerStorage"):WaitForChild("npcbullet")

--Fire
local function BurstOnPlayer(p)
	for c = 1, 3 do
		script.Parent.Gun.Handle.shoot:Play()
		local newb = bullet:Clone()
		newb.Parent = workspace
		newb.CFrame = script.Parent.Gun.Handle.CFrame
	end
	
end

--damaged
script.Parent:WaitForChild("Humanoid").HealthChanged:Connect(function()
	
end)



--path
local path = pfs:CreatePath{
	AgentRadius = 200,
	AgentHeight = 100,
	AgentCanJump = true,
	
}

local hitbox = Instance.new("Part", script.Parent)
local weld = Instance.new("Weld", hitbox)
hitbox.CFrame = script.Parent.HumanoidRootPart.CFrame
weld.Part0 = script.Parent.HumanoidRootPart
weld.Part1 = hitbox
hitbox.CanCollide = false
hitbox.Transparency = 1
hitbox.Size = Vector3.new(100, 100, 100)
local crithitbox = Instance.new("Part", script.Parent)
local weld2 = Instance.new("Weld", crithitbox)
crithitbox.CFrame = script.Parent.HumanoidRootPart.CFrame
weld2.Part0 = script.Parent.HumanoidRootPart
weld2.Part1 = crithitbox
crithitbox.CanCollide = false
crithitbox.Transparency = 1
crithitbox.Size = Vector3.new(60, 60, 60)

spawn(function()
	--LoadAnimation
	local animator = script.Parent:WaitForChild("Humanoid"):WaitForChild("Animator")
	animator:LoadAnimation(Idle):Play()
	while true do
		if not det then
			script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(0, 45), math.random(0, 45), math.random(0, 45)))
			wait(10)
		end
		
		hitbox.Touched:Connect(function(p)
			if p.Parent:FindFirstChild("Humanoid") then
				local fi = game:GetService("Players"):GetPlayerFromCharacter(p.Parent)
				if fi ~= nil then
					print("found a player")
					det = true
					while det == true and lookat == false do
						script.Parent.Humanoid:MoveTo(p.Parent.HumanoidRootPart.Position)
						wait(3)
					end
					
				else
					det = false
				end
			end
		end)
		
		crithitbox.Touched:Connect(function(p)
			if p.Parent:FindFirstChild("Humanoid") then
				local ff = game:GetService("Players"):GetPlayerFromCharacter(p.Parent)
				if ff ~= nil then
					lookat = true
					--script.Parent:SetPrimaryPartCFrame(CFrame.lookAt(script.Parent.HumanoidRootPart.Position, p.Parent.HumanoidRootPart.Position, p.Parent.HumanoidRootPart.Orientation + Vector3.new(180, 0, 0)))
					wait(1)
				end
			else
				lookat = false
			end
		end)
		wait(3)
	end
	
end)
1 Like
local NPC_Root = script.Parent.HumanoidRootPart
local Target_Root = p.Parent:FindFirstChild("HumanoidRootPart")

NPC_Root.CFrame = CFrame.new(NPC_Root.Position,Vector3.new(Target_Root.Position.X, NPC_Root.Position.Y, Target_Root.Position.Z))