local plrs = game.Players
local values = {}
local highestValue = 1

plrs.PlayerAdded:Connect(function(player)
	local inventoryFolder = Instance.new("Folder")
	inventoryFolder.Name = "BackpackInventory"
	inventoryFolder.Parent = player
	
	wait(1)
	while wait(1) do
		if inventoryFolder:FindFirstChild("Sand") then
			
			
			-- Create data for the blocks
			
			local sandData = Instance.new("Folder")
			sandData.Name = "SandData"
			sandData.Parent = inventoryFolder.Sand	
			
			local sandSellWorth = Instance.new("IntValue")
			sandSellWorth.Value = 1
			sandSellWorth.Parent = sandData
			sandSellWorth.Name = "SellWorth"
			
			local sandId = Instance.new("IntValue")
			sandId.Name = "SandId"
			sandId.Parent = sandData
			
			
			
			-- Give the sand an ID
			

			for i, v in pairs(inventoryFolder.Sand:GetChildren()) do
				
				table.insert(values,v.SandId.Value)
				
				for valueNumber, IDvalue in pairs(values) do
					if IDvalue > highestValue then
						highestValue = v
					end
				end
				
			end
			
			sandId.Value = highestValue + 1
			
			highestValue = 0
			values = {}
			
		else
			
			local sandFolder = Instance.new("Folder")
			sandFolder.Parent = inventoryFolder
			sandFolder.Name = "Sand"
			
			local sandData = Instance.new("Folder")
			sandData.Name = "SandData"
			sandData.Parent = inventoryFolder.Sand	

			local sandSellWorth = Instance.new("IntValue")
			sandSellWorth.Value = 1
			sandSellWorth.Parent = sandData
			sandSellWorth.Name = "SellWorth"

			local sandId = Instance.new("IntValue")
			sandId.Name = "SandId"
			sandId.Parent = sandData
			sandId.Value = 1
			
		end
	end	
end)

And in the output it says:

ServerScriptService.Script:40: attempt to compare Instance < number

I've tested it out and it seems highestValue is considered an instance, for some reason. Using tonumber(highestValue) declares it nil.