Make in pairs loop target npc's with specific conditions

  1. What do you want to achieve?
    I want my in pairs loop to get the closest npc to a part, however this npc must have a boolvalue on true

  2. What is the issue?
    I have tried if statements to check if the values were true, but it doesnt seem to work, it gets the closesnt npc with or without the condition

  3. What solutions have you tried so far?
    Nobody seems to be talking about this as far as im concerned, any kind of help would be appreciated

local cola = script.Parent

while true do
	wait(0.5)
	local agro = 2000
for _, npc in ipairs(game.Workspace:GetChildren()) do
	local hum = npc:FindFirstChild("Humanoid")
	local npcval = npc:FindFirstChild("isnpc")
		if hum and npcval.Value == false then
			npcval.Value = true
			if (cola.Position - hum.Parent.HumanoidRootPart.Position).Magnitude < agro then
				agro = (cola.Position - hum.Parent.HumanoidRootPart.Position).Magnitude
				closestnpc = npc
				spawn(function()
					wait(2)
					npcval.Value = false
				end)
			end
	        end
        end
end

Do you have the script where u tried using if true check. It seems to me like it was probably in the wrong place