im trying to locate the nearest player but the output error says that theres an infinite yield possible? how do i fix this?

function getclosestplayer(pos)
local list = game.Players:GetChildren()
local realtorso = nil
local mindist = 1000
local checker = nil
local torsodistance = nil
for i = 1, #list do
	checker = list[i]
	local torso = checker:WaitForChild("HumanoidRootPart")
	if torso then
		torsodistance=(torso.Position - pos).magnitude
		if torsodistance<  mindist then
			realtorso = torso
			mindist = (torso.Position - pos).magnitude
	end
	
	end
	return realtorso.Position
end
end


1 comment

0

The issue is you are searching player for HumanoidRootPart and it is telling you that it's never going to happen...the reason for it is because HRP is in the player's character so instead do checker.Character:WaitForChild("HumanoidRootPart")