Ugh yet again, I have problems with the script :/. First of the script is supposed to index a player that has the smallest distance between a humanoid. If the player dies, or dies, the indexing is repeated all over again and the process resets. I know there are A TON of small problems with the script, like usingmaginitudeto compare the distance between to objects. Here's what I got:

Var = 0
players = nil
difference = 0
player = nil
compare = 0

function HumanoidFollow()
	repeat
		wait(.25)
		workspace.Humanoid.Humanoid.WalkToPoint = player.Character.Torso.CFrame.p
	until player.Character.Humanoid.Health == 0 or player.Character.Humanoid == nil do
		repeat wait() until player.Character.Humanoid.Health == 100
		TableValue()
	end
end


function TableValue()
	players = game.Players:GetChildren()
	for i,v in pairs(players) do
		repeat wait() until v.Character~=nil
		compare = v.Character.Torso.CFrame.p - workspace.Humanoid.Torso.Position
		if v.Character.Humanoid.Health ~= 0 or v ~= nil then
			if compare ~= difference then
				if difference == 0 or compare < difference then
					difference = compare
					player = v
					HumanoidFollow()
				end
			elseif compare == difference then
				player = v
				HumanoidFollow()
			end
		end
	end
end
game.Players.PlayerAdded:connect(TableValue)

I'm sorry if It's hard to understand. The problem is, everything runs fine the first time. Though once the function is called again after the player dies, the output reads: "attempt to compare two UserData values". Except the elseif statement should run and the "HumanoidFollow" function should run. If you give an answer, please only change the stuff that needs fixing. THX!!!