Is there a way to get this npc to look at the closest npc near it?

I’m trying to make this npc (minigunner) look at another npc (zombie), it doesn’t work with the error
13:21:24.687 - Torso is not a valid member of Part
the script for the npc(minigunner)

local tool = script.Parent
local range = script.Parent.Parent.Stats.range.Value
local damage = script.Parent.Parent.Stats.damage
local Bullet = Instance.new("Part", workspace)
local ShootSpeed = script.Parent.Parent.Stats.shootspeed.Value
local Sound = script.Parent.Sound
local Radius = Instance.new("Part", workspace)
local TestEvent = game.ReplicatedStorage.Events.TestEvent
local Rotate = 3
Radius.CanCollide = true
Radius.Parent = tool.Parent
Radius.Position = script.Parent.Parent["Left Leg"].Position
Radius.Position = Radius.Position + Vector3.new(-3,0.025,0)
Radius.Anchored = true
Radius.Size = Vector3.new(10,0,10)
Radius.Name = "MinigunRadius"
function shoot()
	Bullet.Transparency = 0
	Bullet.CanCollide = true
	Bullet.Position = script.Parent.Handle.Position
	Bullet.Anchored = true
	Bullet.Name = "MiniGun1Bullet"
	Bullet.BrickColor = BrickColor.new("New Yeller")
	Bullet.TopSurface = Enum.SurfaceType.Smooth
	Bullet.BottomSurface = Enum.SurfaceType.Smooth
	Bullet.Size = Vector3.new(0.3,0.1,0.1)
	Bullet.Orientation = Vector3.new(0, 90, 0)
	Bullet.Parent = tool
	for yes = 1,range do
		wait()
		local function closestEnemyFunction()
			local ClosestEnemy = tool.Parent.Stats.ClosestEnemy
			game.Workspace.Minigunner.Torso.CFrame = CFrame.new(game.Workspace.Minigunner.Torso.Position, ClosestEnemy.Value.Torso.Position * Vector3.new(1,0,1) + game.Workspace.Minigunner.Torso.Position * Vector3.new(0,1,0))
		end
		TestEvent.Event:Connect(closestEnemyFunction)
		if Rotate == 1 then
			Bullet.Position = Bullet.Position + Vector3.new(0,0,-1)
		end
		if Rotate == 2 then
			Bullet.Position = Bullet.Position + Vector3.new(0,0,1)
		end
		if Rotate == 3 then
			Bullet.Position = Bullet.Position + Vector3.new(-1,0,0)
		end
		if Rotate == 4 then
			Bullet.Position = Bullet.Position + Vector3.new(1,0,0)
		end
	end
	Bullet.Transparency = 1
	Bullet.CanCollide = false
		end
while wait(ShootSpeed) do
	Sound:Play()
	shoot()
	end

the script for the other npc(zombie)

FastZombie = script.Parent
local TestEvent = game.ReplicatedStorage.Events.TestEvent
function onTouched(hit)
	if (hit.Parent.Name == "Minigunner") then
		print('touched minigun radius')
		local NameValue = Instance.new("ObjectValue", game.Workspace.Minigunner.Stats)
		NameValue.Name = 'ClosestEnemy'
		NameValue.Value = FastZombie.Torso
		TestEvent:Fire()
	   end
end
connection = FastZombie["Left Leg"].Touched:connect(onTouched)

if this script matters, script for the waves

local physicsService = game:GetService("PhysicsService")
physicsService:CreateCollisionGroup("Zombies")
physicsService:CollisionGroupSetCollidable("Zombies", "Zombies", false)
local Zombie = game.ReplicatedStorage.Enemies["Normal Zombie"]
local FastZombie = game.ReplicatedStorage.Enemies["Fast Zombie"]
local BigZombie = game.ReplicatedStorage.Enemies["Big Zombie"]
local MarbleZombieBoss = game.ReplicatedStorage.Enemies["Marble Zombie"]
local Part1 = game.Workspace.Part1
local Part2= game.Workspace.Part2
local Part3 = game.Workspace.Part3
local Part4 = game.Workspace.Part4
local Part4 = game.Workspace.Part4
local Part5 = game.Workspace.Part5
local Part6 = game.Workspace.Part6
local Part7 = game.Workspace.Part7
local Part8 = game.Workspace.Part8
local dead = false
function wave(zombieType, numberOfZombies)
	for index = 1, numberOfZombies do
		spawn(function()
			local ZombieClone = zombieType:Clone()
			for _, part in ipairs(ZombieClone:GetDescendants()) do
				if part:IsA("BasePart") then
					physicsService:SetPartCollisionGroup(part, "Zombies")
				end
			end
			ZombieClone:SetPrimaryPartCFrame(CFrame.new(-108.33, 0.5, -27.21))
			ZombieClone.Parent = workspace
			wait(1)
			ZombieClone.Humanoid:MoveTo(Part1.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:MoveTo(Part2.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:MoveTo(Part3.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:MoveTo(Part4.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:MoveTo(Part5.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:MoveTo(Part6.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:MoveTo(Part7.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:MoveTo(Part8.Position)
			ZombieClone.Humanoid.MoveToFinished:Wait()
			ZombieClone.Humanoid:Destroy()
			wait(1)
			ZombieClone:Destroy()
		end)
		wait(.10) -- Delay Inbetween Spawning Zombies
	end
end
wave(FastZombie, 5)
wave(Zombie, 5)
wave(BigZombie,2)
wave (MarbleZombieBoss, 1)

To make something like a humanoidrootpart (i shall be calling hrp) rotate towards another npc, you can use CFrame.new(hrp.Position, zombiehrp.Position) to make it point and the zombie.
In addition, there error you are getting is because you have the wrong value or the same name for parts.
Moving the torso will cause the rig to break as well I believe.

1 Like

the npc’s don’t have a humanoidrootpart

If the NPC’s dont have a humanoid root part then the rig shouldn’t be able to move and the humanoid wont work? What is the humanoid’s .RootPart?

1 Like

Oh, I didn’t see that, the zombie has a humanoid root part, but the minigunner doesn’t

1 Like

I believe your problem is you were looking for the torso inside of the torso as well

local NameValue = Instance.new("ObjectValue", game.Workspace.Minigunner.Stats)
		NameValue.Name = 'ClosestEnemy'
		NameValue.Value = FastZombie.Torso