How to fix: pathfinding

Hi! I am making a simple pathfinder and I am wondering: Why doesn’t this script work? At the part where it checks if a part is closer to the endpoint or not, the IF statement seems to think that higher decimals are the same as lower ones because the rounded version (14) is still 14. Please help me fix this. Thanks.

Code (server script):

repeat wait()
	run += 1
	local e = Instance.new("Part",workspace.e)
	e.Anchored = true
	e.CanCollide = false
	e.Size = Vector3.new(0.1,0.1,0.1)
	e.Name = run

	e.Position = pos + Vector3.new(0,0,0.1)

	local e = Instance.new("Part",workspace.e)
	e.Anchored = true
	e.CanCollide = false
	e.Size = Vector3.new(0.1,0.1,0.1)
	e.Name = run

	e.Position = pos + Vector3.new(0.1,0,0)

	local e = Instance.new("Part",workspace.e)
	e.Anchored = true
	e.CanCollide = false
	e.Size = Vector3.new(0.1,0.1,0.1)
	e.Name = run

	e.Position = pos + Vector3.new(0,0,-0.1)

	local e = Instance.new("Part",workspace.e)
	e.Anchored = true
	e.CanCollide = false
	e.Size = Vector3.new(0.1,0.1,0.1)
	e.Name = run

	e.Position = pos + Vector3.new(-0.1,0,0)

	for _, i in pairs(workspace.e:GetChildren()) do
		wait()
		local closest = 999999
		local mag
		
		mag = (i.Position - workspace.endd.Position).Magnitude
		if mag <= closest then
			closest = mag
			pos = i.Position
			i.Name = mag
			--i:Destroy()
			--print(mag)
			--i:Destroy()
		else
			i:Destroy()
		end
		print(mag,closest)
	end
	
	--if e:FindFirstChild(run) then
	--	pos = e.Part.Position
	--	--e.Part:Destroy()
	--end
until pos == game.Workspace.endd.Position