Looping Function Producing Errors

I just rewrote the whole thing (there looked to be quite a few unnecessary or unused variables)

local Root = game:GetService("Players").LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local function GetClosest()
	local closest = {nil, math.huge}
	for i,v in pairs(game:GetService("Players"):GetPlayers()) do
		if v.Character ~= nil and v.Character:FindFirstChild("Ball") then
			if (Root.Position - v.Character.Ball.Position).Magnitude < closest[2] then
				closest = {v, (Root.Position - v.Character.Ball.Position).Magnitude}
			end
		end
	end
return closest[1]
end

(Depending on the location of the Ball , you may need to change the location on line 5.

1 Like