So I was making an NPC that would turn its torso towards you when you come in a distance of 30 studs. Here's my script: (ServerScript in NPC model)
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
player.CharacterAdded:Connect(function(character)
while not character do
character.AncestryChanged:Wait()
end
local hrp = character:WaitForChild('HumanoidRootPart')
local hum = character:WaitForChild('Humanoid')
local enemyHum = script.Parent.EnemyHum
local enemyHRP = script.Parent.HumanoidRootPart
local enemyHRPcf = enemyHRP.Position
local dead = false
local inRange = false
hum.Died:Connect(function()
dead = true
end)
while wait() do
if (enemyHRP.Position - hrp.Position).Magnitude < 30 then
inRange = true
script.Parent.Pistol.Handle.fire:Play()
local bulletPart = workspace.bullet:Clone()
bulletPart.Parent = workspace
bulletPart.Position = script.Parent.Pistol.Handle.Position
bulletPart.Transparency = 0
bulletPart.Anchored = true
bulletPart.CanCollide = false
bulletPart.Orientation = (enemyHRP.Orientation - hrp.Orientation)
enemyHRP.Orientation = (enemyHRP.Orientation - hrp.Orientation)
for i = 0,25,0.5 do
bulletPart.CFrame = bulletPart.CFrame * CFrame.new(0,0,i)
wait()
bulletPart.Touched:Connect(function(hit)
local t = true
if hit.Parent:WaitForChild('Humanoid') then
if t == true then
t = false
hum:TakeDamage(8)
bulletPart:remove()
end
end
end)
end
bulletPart:remove()
elseif (enemyHRP.Position - hrp.Position).Magnitude > 30 then
inRange = false
end
end
end)
However, when I did come within 30 studs, the NPC started missing entirely, like a stormtrooper. I later found out it was actually spinning and that was the reason of having bad aim.
It's supposed to work, but it's not..?
Help please!

4 comments
i mean if its spinning, that probably would have to do with orientation right?
@chloe_price65 Yes, it's actually spinning and I use orientation.
Try using CFrame lookvector.
Yea, I solved it. Thanks for helping though.