Problems about my hiding system

This is the script:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

local function loop()
	while true do
		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
		wait(math.random(1,5))
		if target ~= nil then
			script.Parent.Humanoid:MoveTo(target.Position, target)
		end
		return target
	end
end

local targ = loop()
if targ.Parent:FindFirstChild("isHiding").Value == false then
	loop()
elseif targ.Parent:FindFirstChild("isHiding").Value == true then
	return loop()
end

Issues?
The problem is it errors…
The error: Workspace.Character.Script:39: attempt to index nil with ‘Parent’

Looks to me like targ is referring to loop() itself, not any instances gotten from loop(), therefore it would not have a parent.