I need help debugging and improving this entire script

Click to see script
local parent = script.Parent
local damage = 9438594358345934589348959834583495834985

hum = parent:FindFirstChildWhichIsA("Humanoid")
root = parent:FindFirstChild("HumanoidRootPart") or parent:FindFirstChild("Torso")

local function TorsoFinding(origin: vector3): Vector3?
	local nearestPosition: Vector3 = nil;
	local smallestDistance: number = math.huge; -- DISTANCE

	for _, human in workspace:GetDescendants() do --GETTING PLAYERS
		--print("ACTIVE")
		if human.Name ~= parent.Name and human:FindFirstChild("NPCTAG") == nil then --PREVENTS NPCS FROM BEING TARGETTED
			if human.ClassName == "Model" then 
				local hrp = human:FindFirstChild("HumanoidRootPart")
				if hrp then
					local distance = (hrp.Position - origin).Magnitude -- TRACKING PLAYER HUMANOID

					if distance < smallestDistance then
						smallestDistance = distance;
						nearestPosition = hrp.Position;
					end
				end
			end
		end
	end

	return nearestPosition
end

local function Torsos()
	local torso = {}
	for _, human in workspace:GetDescendants() do --GETTING PLAYERS
		--print("ACTIVE")
		if human.Name ~= parent.Name and human:FindFirstChild("NPCTAG") == nil then --PREVENTS NPCS FROM BEING TARGETTED
			if human.ClassName == "Model" then 
				local humanoid = human:FindFirstChild("Humanoid")
			end
		end
	end
	return torso
end

for _, humanParts in pairs (parent:GetChildren()) do
	if humanParts.ClassName == "Part" then
		humanParts.Touched:Connect(function(hit)
			if hit.Parent == workspace then
				if hit:FindFirstChild("Humanoid") then
					hit:FindFirstChild("Humanoid").Health -= damage
					print("HUMAN TOUCHED.")
				end
			end
		end)
	end
end

while hum.Health > 0 do
	local torsopos = TorsoFinding(root.Position)
	local foundTorsos = Torsos()
	while torsopos do
		if foundTorsos.Health > 0 then
			hum:MoveTo(torsopos .Position * Vector3.new(root.Position, torsopos.Position).LookVector * 6)
		else
			break
		end
	end
	task.wait()
end

For context, this is a AI chasing script. A NPC will chase after a nearby humanoid ( and human:FindFirstChild("NPCTAG") == nil prevents the model AI from chasing other humanoids that aren’t players. If this was removed, the model would chase after after both players and other npcs with humanoids) should they be in range. It does not rely on Pathfinding.

However, there are many, many, many errors. Many of them start with finding the player (hum:MoveTo(npp.Position * Vector3.new(root.Position, npp.Position).LookVector * 6),

Workspace.HIMBOSS.AI:61: attempt to compare number < nil  -  Server - AI:61
  15:31:09.491  Stack Begin  -  Studio
  15:31:09.491  Script 'Workspace.HIMBOSS.AI', Line 61  -  Studio - AI:61
  15:31:09.491  Stack End 

and the functions itself, best primarily these 2.

local function TorsoFinding(origin: vector3): Vector3?
	local nearestPosition: Vector3 = nil;
	local smallestDistance: number = math.huge; -- DISTANCE

	for _, human in workspace:GetDescendants() do --GETTING PLAYERS
		--print("ACTIVE")
		if human.Name ~= parent.Name and human:FindFirstChild("NPCTAG") == nil then --PREVENTS NPCS FROM BEING TARGETTED
			if human.ClassName == "Model" then 
				local hrp = human:FindFirstChild("HumanoidRootPart")
				if hrp then
					local distance = (hrp.Position - origin).Magnitude -- TRACKING PLAYER HUMANOID

					if distance < smallestDistance then
						smallestDistance = distance;
						nearestPosition = hrp.Position;
					end
				end
			end
		end
	end

	return nearestPosition
end

local function Torsos()
	local torso = {}
	for _, human in workspace:GetDescendants() do --GETTING PLAYERS
		--print("ACTIVE")
		if human.Name ~= parent.Name and human:FindFirstChild("NPCTAG") == nil then --PREVENTS NPCS FROM BEING TARGETTED
			if human.ClassName == "Model" then 
				local humanoid = human:FindFirstChildWhichIsA("Humanoid")
			end
		end
	end
	return torso
end

for _, humanParts in pairs (parent:GetChildren()) do
	if humanParts.ClassName == "Part" then
		humanParts.Touched:Connect(function(hit)
			if hit.Parent == workspace then
				if hit:FindFirstChild("Humanoid") then
					hit:FindFirstChild("Humanoid").Health -= damage
					print("HUMAN TOUCHED.")
				end
			end
		end)
	end
end

I’ll explain what’s wrong with them. First one that I want to go over on is this line of code.

for _, humanParts in pairs (parent:GetChildren()) do
	if humanParts.ClassName == "Part" then
		humanParts.Touched:Connect(function(hit)
			if hit.Parent == workspace then
				if hit:FindFirstChild("Humanoid") then
					hit:FindFirstChild("Humanoid").Health -= damage
					print("HUMAN TOUCHED.")
				end
			end
		end)
	end
end

When a NPC/Player touches the parent, it does not damage the player to the point where they are instantly killed.

For the functions revolving around finding torsos, at this point,

while hum.Health > 0 do
	local torsopos = TorsoFinding(root.Position)
	local foundTorsos = Torsos()
	while torsopos do
		if foundTorsos.Health > 0 then
			hum:MoveTo(torsopos .Position * Vector3.new(root.Position, torsopos.Position).LookVector * 6)
		else
			break
		end
	end
	task.wait()
end

The foundTorso function does not find the Humanoid.Health. I do not know how to specifically get the value/humanoid for cross communication into the while hum.Health > 0 do part. I do not know what to change in each and all of the functions so that it can properly work. That’s why I’m asking for help to both debug and improve this script. I don’t know what’s wrong. I can’t figure out the solutions due to my lack of knowledge on how to make a humanoid move with PathFinding Service (Even with pathfinding service). If anyone would like to do, please do so! I don’t know what to do.

1 Like