How do I make a check outside of the loop I need?

Script:

local npc = script.Parent
local debounce = false
local folder = script.Parent:WaitForChild("Information")

npc.Humanoid.Touched:Connect(function(hit) -- DAMAGING PLAYER
	if hit and game.Players:GetPlayerFromCharacter(hit.Parent) and folder.Attacks.Value == true then
		if debounce == false then
			debounce = true
			hit.Parent.Humanoid:TakeDamage(10) -- U SHOULD TO CHANGE THIS!!!
			wait(.25) -- AND THIS!!!
			debounce = false
		end
	end
end)

function FindPlayer(Position)
	local playerList = game:GetService("Players"):GetPlayers()	
	local torso = nil
	local distance = 30 -- CHANGE THIS TOO!!!
	local HRP = nil
	local humanoid = nil
	local player = nil
	
	for i, plr in pairs(playerList) do
		local playerCharacter = plr.Character or plr.CharacterAdded:Wait()
		if playerCharacter then
			HRP = playerCharacter:FindFirstChild("HumanoidRootPart")
			humanoid = playerCharacter:FindFirstChild("Humanoid")
			
			if (HRP ~= nil) and (humanoid ~= nil) and (humanoid.Health > 0) then
				
				if (HRP.Position - Position).Magnitude < distance then
					torso = HRP
					distance = (HRP.Position - Position).Magnitude
					print(distance)
					folder.Distance.Value = distance -- CHANGING DISTANCE!!!
				end
			end
		end
	end
	
	return torso
end

while wait(1) do
	local target = FindPlayer(script.Parent.HumanoidRootPart.Position)
	
	npc.Humanoid.Health = 100 -- DELETE THIS LATER
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target) -- MOVING NPC TO US
	end
end

As I understand it, the problem is that I can’t check the distance when it’s over 30
And I have to check the distance somewhere else, but I don’t know where.

This might need to be changed…

move the distance variable into a different scope. So move it outside the function so it can be used and changed in both parts