I was creating some NPC's that shoot you when you approach too near. I made this simple ServerScript inside of the NPC:

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)

Okay, it might not be simple but, when I test it, the NPC's just spin around, completely missing their target.

This is supposed to work, but it isn't.

??? Help please!